faqts : Computers : Programming : Languages : JavaScript : Images

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

23 of 27 people (85%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I load a different IMG depening on the time of the day?

Feb 12th, 2000 17:37
Martin Honnen,


Use 
  var now = new Date()
to get the current time and then document.write the IMG element 
depening on the current hour:

  <SCRIPT>
  var dayImage = 'day.gif';
  var nightImage = 'night.gif';
  var now = new Date();
  var hour = now.getHours();
  if (hour >= 8 && hour <= 20)
    document.write('<IMG SRC="' + dayImage + '">');
  else
    document.write('<IMG SRC="' + nightImage + '">');
  </SCRIPT>