homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Script to show a different image based on the month?
Such a Javascript would always make my site look up-to-date!

 3:01 pm on May 20, 2002 (gmt 0)

I already have a javascript to show different text based on the month, but I was wondering if anybody could refer me to a script which showed a different image for each month?

I recall seeing one a couple of years back where the script displayed a different graphic based on the month e.g. imagejanuary.gif imagefebruary.gif

Thank you :)



 6:28 pm on May 20, 2002 (gmt 0)

<script language="javascript" type="text/javascript">
var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var thetime = new Date();
var themonth = thetime.getMonth();
document.write('<img src="image' + months[themonth] + '.gif" alt="Image of the month: ' + months[themonth] + '" />');

use image names like 'imageMay' and it will work just fine ;)


 8:59 pm on May 20, 2002 (gmt 0)

I'm probably not your typical browser, but to me such things just make a site look like it has Javascript (or PHP, ASP, whatever) that wastes bandwidth, especially since it is often obvious that the real content is hopelessly out of date. I think

"Page last updated: [date of last serious, major update]"

is useful, but

"Today's date is: [today's date]"

or worst of all

"Page last updated: [today's date or date last typo was fixed]"

are useless at best and will drive users away at worst since your credibility will be compromised. I hope that doesn't sound too negative. You know what they say about opinions....



 11:30 pm on May 20, 2002 (gmt 0)

Thank you VERY much DrDoc. I appreciate your help.

'ergophobe', I see your point. I work 12 hours a day/7 days a week to keep my sites up-to-date. However, I am trying to automate certain things so that I'll have the chance to leave me some time to occasionally take a few steps away from my PC :)

If I use such a script alongside similar text links, I could promote seasonal offers on my sites without having to make the changes manually? e.g. for my movie site: February = Romantic movies; October = Halloween movies; December = Christmas promotion.

Don't you think that is a good idea to automate such advertising and save myself time?


 12:46 am on May 21, 2002 (gmt 0)

You're most welcome 1Lit! :)

I agree, it's good idea to let a script take care of 'updates' like that. I used to have a 'joke of the day' on one of my Web sites .. People loved it, even though they knew that it was automated.

But, ergophobe, I see your point. It is totally useless (will even have a negative impact on the visitor) to use JavaScript to 'fake' updates.

To have dynamic content, depending on the day or month, can be useful, if it has a purpose and if you used your brain when you thought of the purpose. ;)


 1:35 am on May 21, 2002 (gmt 0)

I've managed to do some quite nifty things with your script DrDoc, so thank you once again.

Just one other thing I wanted to ask: is there any way of making an ammendment to your script so that I can send the surfer to a different URL depending on the month? I've decided I'd like to send my visitors to a weight-loss site in January, flower-selling site in February, my Christmas store in December etc. :)



 3:09 am on May 21, 2002 (gmt 0)

Don't you think that is a good idea to automate such advertising and save myself time?

Most definitely. That's why I was worried about sounding too negative, but I think you both took my point in the sense that it was intended. Dynamic content that is fed based on certain conditions is wonderful and worth the effort. I have just seen so many sites where the designer uses Javascript to *fake* freshness. It can be like the "NEW!!" label that identifies something from three years ago.

Dynamic methods of *creating* real fresh pages that give the user something appropriate is great, especially if you sell things that are themed (Valentine's day stuff in your case). Joke of the day can be great too.




 3:55 pm on May 21, 2002 (gmt 0)


Just use the document.write() function and customize it for your needs.

Example: document.write('<a href="' + months[themonth] + '.html">Link text goes here</a>');

And you know what? You can re-use the months[themonth] variable as often as you want. If you want several 'customized' links and images and what not .. just make sure that the first script I gave you is somewhere near the top of your page, preferrably in the HEAD section. Then, whenever you want to use it, just add another script that only contains the document.write() statment. Your browser will remember the value of the months[themonth] variable ;)

For redirection, just use this syntax:
location.href = months[themonth] + ".html";

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved