Thursday, 30 April 2009

Javascript to Change Background Based on the Day of the Year

This Javascript will change the background image based on the day of the year.

It expects there to be 366 images to change to called 001.jpg, 002.jpg ... 366.jpg.



<h1>Day of Year Background</h1>

<script type="text/javascript">

var firstJan = Math.floor((new Date().setFullYear(new Date().getFullYear(),0,1))/86400000);
var today = Math.ceil((new Date().getTime())/86400000);
var dayOfYear = today-firstJan;

var bgdImage;
if((dayOfYear+'').length == 1)
bgdImage = '00'+dayOfYear+'.jpg';
else if((dayOfYear+'').length == 2)
bgdImage = '0'+dayOfYear+'.jpg';
bgdImage = dayOfYear+'.jpg';"url(\""+bgdImage+"\")");




For articles on changing backgrounds on a daily basis or based on the season, See Daily Background Image Change or Seasonal Background Image Change here.

To change the background using Javascript see here


No comments: