Any Google users cannot fail to see that the image changes on a daily basis (if not more) and they always show something appropriate for the time of year; for the Christmas lead-up the image may be Father Christmas , or a snowman, or just a Christmas scene.
For a few days before Valentine's Day there may be hearts and roses. For New Year's day there may be scythes or party hats. Of course that doesn't necessarily mean that there's a Google web page developer continually logging on and making changes - it just means that the Google web site is a date aware application; and that's something that any web page can produce with Javascript.
Creating a Date Aware Web Page
The date aware web page will need to:
- calculate the current date
- calculate if a key day of the year is approaching
- display a random picture that is appropriate for that key date
- for any other day display one of set of default images
and so the web page may display (for example) one of three Christmas scenes for 7 days before Christmas.
Adding an Image Tag to a Web Page
A image tag can easily be added to a web page by using HTML (HyperText Markup Language):
In this case no actual image is defined - that will be done by using Javascript.
Displaying Random Images with Javascript
In this example lists of the images to be displayed are stored in a number of arrays:
The actual number of images does not matter because Javascript will generate a random number according to the length of the array passed to it:
This function is run by using the Javascript code:
This, of course, will display one of the images listed and so the next stage is to select the array according to the time of year.
Deciding on the Day of the Year
The code for calculating the day of the year is quite simple - it subtracts the current date from January 1st (although the answer is initially in milliseconds):
The result is the day of the year for the inputted date.
Displaying Images for the Day of the Year
The starting point for this next function is to calculate the day of the year for each of the key dates:
Then, depending how close the current day of the year is to a key day of the year, the appropriate array is sent to the random_image function:
This can then be run from the web page:
And then the web page programmer can leave the web page safe in the knowledge that, regardless of the time of year, the appropriate images will be displayed.