Select a different inspiring quote every hour

Wouldn't it be great to add a touch of inspiration to my page with a new motivational quote that changes every hour or day? I'm thinking of compiling all the quotes in a document and having javascript randomly select one to display on my website. Maybe using a list format like this:

<ul>
   <li>It’s not that I’m so smart, it’s just that I stay with problems longer.</li>
   <li>Eighty percent of success is showing up.</li>
   <li>I respect faith, but doubt is what gets you an education.</li>
   <li>...</li>
<ul>

Is there a way to automate this process, or do I have to manually update the quotes (perhaps through a CMS or directly in HTML)?

Answer №1

It is entirely possible to display random messages without using any server-side code, but keep in mind that without it, there is no way to ensure the same message will be shown to every visitor or even the same visitor in different browser instances. One approach could be selecting a random message from a predefined set each time someone visits your page. Here's an example:

var phrases = [
    {
      hour: 0,
      content : [
        "Be or not to be",
        "Foo",
        "Bar"
      ]
    },
    {
      hour: 1,
      content : [
        "Be or not to be",
        "Foo",
        "Bar"
      ]
    }
];

var phrase = undefined; //get one randomly based on new Date().getHours()
document.addEventListener('DOMContentLoaded', function() {
   document.getElementById('phrase').innerHTML = phrase;
});

For a complete example, visit:

https://plnkr.co/edit/tBcH5DYxYTxl2kBtp0yk?p=preview

This is just a suggestion and I hope it proves helpful to you. If you want everyone to see the same message, consider having only one message per hour. This way, you can achieve a consistent experience without relying on server-side logic. Keep in mind that this approach will show the same set of messages every day.

Answer №2

Instead of choosing a new quote every hour or day, you could have a different quote displayed each time the page is refreshed. One way to achieve this is by storing the quotes in a table and assigning each quote an id. Then, on your webpage, you would connect to the database and fetch a random id using the following function:

int rand ( int $min , int $max )

This function would return an integer between the specified minimum and maximum values ($min and $max), which in this case would be the range of ids. The corresponding quote for that randomly selected id can then be displayed on the page. Please note that while this suggestion may not directly answer your question, it offers a possible approach to achieving your desired outcome. (I'm unable to comment due to my lack of reputation at the moment!)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the class based on the number of li elements

When there are more than two (li) tags, the ul tag should have the class "col_3"; otherwise it should have the class "col_2" For instance: If there are two columns <div class="container"> <ul class="col_2"> <li>One</li> ...

The scripts within the body tag are failing to load

After trying to embed angular into the body tag, I noticed that nothing is loading up. Upon inspecting the resources panel, I found that only files from the head are present. Moving all the scripts to the head section resolves the issue and everything load ...

Is there a way to adjust the background color of the clicked tab with divs and revert the others back to their original color?

<span class="row top-bar-left align-items-center" style="width: 80%; float:left"> <div tabindex="1" class="link tab" routerLink="details" routerLinkActive="active" [qu ...

The placement of a material-ui component at the beginning of the render method can impact the styling of the following components in the hierarchy

After creating some styled components, I noticed that they were not rendering correctly for me. However, when I transferred the code to a codesandbox environment (which had a material-ui button in the template), the components rendered perfectly fine. It w ...

The responsive design of Bootstrap NavBar seems to be malfunctioning when viewed on an iPhone device

Having an issue in Bootstrap Studio that I can't seem to resolve. I've seen recommendations to add the following meta tag to the <head> section: meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0", bu ...

Adjust the font size to fit the size of the button when resizing a md-button

Currently utilizing md-button with text inside An issue arises in low resolution where the text gets cut off, struggling to find a suitable solution If anyone has any suggestions on how to resolve this problem, please share. I have included screenshots ...

developing a deferred commitment by utilizing promise objects

Hi everyone, I've encountered an issue with a JavaScript promise question that's throwing errors function delay(n) { return new Promise((resolve) => setTimeout(resolve, n*1000)); } The expected output should be "It is now 2 seconds later" ...

php print some additional php script

I am struggling with implementing PHP code that contains an echo statement. However, I need this echo to output PHP code within it. Here is a brief example: <!php echo '<?php echo "test"; ?>'; ?> The main issue arises when I try to ...

There was an error in the search: [parse_exception] The search source could not be parsed. A field name was expected, but instead [

I'm experiencing a query parsing exception while utilizing JavaScript for Elasticsearch configuration found in elastic.js file. Results are obtained when the filtered part is removed. However, upon adding it back, an exception occurs. var client = r ...

The border color of the text input is not changing as expected when in focus

Is there a way to change the border color of a text input field to blue when it is not selected and then change it to orange when the field is selected? input[type="text"]{ border: 1px solid blue; } input[type="text"]:focus{ border: 1px solid ora ...

Verify if the input field is devoid of any content or not

I am planning to create a validation form using Vanilla JavaScript. However, I have encountered an issue. Specifically, I want to validate the 'entername' field first. If the user does not enter any letters in it, I would like to display the mess ...

Error: Attempting to access 'push' property on an undefined object

Encountered an Error After implementing the useNavigate function, I successfully resolved the issue. let params = useParams(); let navigate = useNavigate(); const dispatch = useDispatch(); const productId = params.id; const [qty, setQty] = useStat ...

looping through an ajax function with addEventListener

Apologies in advance for any errors in my English. My task involves creating a simple webpage with just 2 links. When one of these links is clicked, it should load the content of a specific .html file on the same page. For example: clicking on link 1 (wi ...

Ways to modify the color of the legend text in a HighChart graph

Click here https://i.sstatic.net/iuPs4.png I am attempting to modify the color of the series legend text from black to any color other than black: $(function () { $('#container').highcharts({ legend: { color: '#FF0000', ...

The issue with CSS z-index not functioning properly when using absolute positioning

Here is the issue I'm facing: I have multiple div containers in my code. .outer { position: relative; z-index: 1; } .inner { position: absolute; z-index: 999; } <div class="outer"> <div class="inner"> <div class="option ...

Establishing properties while creating a fresh instance of a class

I am currently working on developing an invoice application using TypeScript. In my project, I have created an Invoice class with several properties and objects. However, when attempting to set the properties of an item object within the Invoice class usin ...

Count the start and end rows of a table within the <tfoot> using pdfHTML/iText 7

Currently, I am working on a project where I am developing a document generator that utilizes pdfHTML 3.0.3 and iText 7.1.14. The document includes a table displaying 'items'. It's worth noting that these item rows will likely span across mu ...

Is the whitespace-pre-line feature of TailwindCSS experiencing issues?

whitespace-pre-line doesn't seem to be working for me. I attempted to replicate the same code from my text editor on https://play.tailwindcss.com/, and it worked perfectly. Below is a screenshot that I have attached. This is the sample code in my tex ...

Listcell XUL with button options

How can I make buttons inside a listcell function in XUL? I am having trouble getting it to work. Here is the XUL code: <listitem id = "1"> <listcell label = "OK Computer"/> <listcell label = "Radiohead"/> <listcell label ...

What is the process for translating HTML elements into JSX format?

I have a couple of inquiries. 1.) I am working on a basic reactjs component. How can I transform it into JSX? I want the code to follow the reactjs style. 2.) If I receive an array from the backend in this format [ '/parent-folder-1/child-folder ...