PDA

View Full Version : Seasonal random image rotator



Christiane
05-14-2010, 10:26 AM
Hello, I don't know much about JavaScript but am trying to build a random image rotator that displays a different image every time the page loads but also depends on the season or month (if that's easier). I found this script online and it works fine in general:

var imageArray = new Array();

imageArray[0] = "spring1.jpg";
imageArray[1] = "spring2.jpg";
imageArray[2] = "spring3.jpg";
imageArray[3] = "spring4.jpg";
function doIt() {
var rand = Math.floor(Math.random() * 4);
var imgPath = "<img src='" + imageArray[rand] + "' alt='heder' border='0' align='absmiddle' />";
document.getElementById("seasonal").innerHTML = imgPath;
}

Is it possible to have 4 folders (spring, summer, autumn, winter) with different images and then call a function that first checks what season it is and then displays the images for the right season/ from the right folder at random?

Christiane
05-14-2010, 11:21 AM
I found a friend who knew. If anyone else ever searches for a solution, this is the code:

var imageArray = new Array();
var header = '';
var now = new Date();
var month = now.getMonth() + 1;


switch(month)
{
case 12 :
case 1:
case 2:

imageArray[0] = "images/flash/winterText1.jpg";
imageArray[1] = "images/flash/winterText2.jpg";
imageArray[2] = "images/flash/winterText3.jpg";
imageArray[3] = "images/flash/winterText4.jpg";
header = 'Winter';
break;

case 3:
case 4:
case 5:

imageArray[0] = "images/flash/springText1.jpg";
imageArray[1] = "images/flash/springText2.jpg";
imageArray[2] = "images/flash/springText3.jpg";
imageArray[3] = "images/flash/springText4.jpg";
header = 'Spring';
break;

case 6:
case 7:
case 8:

imageArray[0] = "images/flash/summerText1.jpg";
imageArray[1] = "images/flash/summerText2.jpg";
imageArray[2] = "images/flash/summerText3.jpg";
imageArray[3] = "images/flash/summerText4.jpg";
header = 'Summer';
break;

case 9:
case 10:
case 11:

imageArray[0] = "images/flash/autumnText1.jpg";
imageArray[1] = "images/flash/autumnText2.jpg";
imageArray[2] = "images/flash/autumnText3.jpg";
imageArray[3] = "images/flash/autumnText4.jpg";
header = 'Autumn';
break;
}

function doIt() {
var rand = Math.floor(Math.random() * 4);
var imgPath = "<img src='" + imageArray[rand] + "' alt='" + header + "' border='0' align='absmiddle' />";

document.getElementById("seasonal").innerHTML = imgPath;

}