Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q236 How can I show a different image each day?

irt.org | Knowledge Base | JavaScript | Image | Q236 [ previous next ]

Q236 How can I show a different image each day?

What we need is some mechanism to choose the image to be displayed. The following script will always show 1.jpg:

<script language="JavaScript"><!--
document.write('<img src="images/' + 1 + '.jpg">');
//--></script>

This is similar to:

<script language="JavaScript"><!--
var number = 1;
document.write('<img src="images/' + number + '.jpg">');
//--></script>

Now all we need to do is alter the value of the number. It would be easier if you had seven different images. That way we can display a different image for each day of the week:

<script language="JavaScript"><!--
var today = new Date();
var number = today.getDay() + 1;
document.write('<img src="images/' + number + '.jpg">');
//--></script>

This creates a date object and then gets the number of the day of the week, and then uses that to display the relevant image.

If you have more than 7 images then you'll need to alter this, either using the day of the month or the day of the year, or maybe even the day of the millennium!

Feedback on 'Q236 How can I show a different image each day?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.