JavaScript : Random Image Function!
Posted: Tue Apr 15, 2003 10:11 am
I have and come up with a Javascript function to randomize image selection the only problem with it was that it would pick the some picture for all five pictures that are the on the page. To explain further I have the five pictures that are listed in the array displayed on the screen and wanted to randomized them! The code I have pasted below is my lastest go at trying to get around the problem but it doesn't work it still displays more than one picture of the same person!
Also I haven't try this yet but I want the function to automaticly change the pics and not on a page reload but I think that setting timers in JavaScript is went to be easy
. I have used timers in PHP so I hope that I can use them in JavaScript but any help would be very welcome!
If it's not asking to much I would like any code pasted for help given explain to some (like me) who doesn't understand JavaScript very well.
Thank You, Glenn Curtis
My Function ::
function RandomBuffy() {
BuffyArray = new Array (
"images/home/buffy.jpg",
"images/home/xander.jpg",
"images/home/willow.jpg",
"images/home/spike.jpg",
"images/home/dawn.jpg");
RandomPic_B = Math.floor(BuffyArray.length * Math.random());
RandomPic_X = Math.floor(BuffyArray.length * Math.random());
RandomPic_W = Math.floor(BuffyArray.length * Math.random());
RandomPic_S = Math.floor(BuffyArray.length * Math.random());
RandomPic_D = Math.floor(BuffyArray.length * Math.random());
if (RandomPic_B != RandomPic_X || RandomPic_W || RandomPic_S || RandomPic_D) {
document.Buffy.src = BuffyArray[RandomPic_B]
document.Xander.src = BuffyArray[RandomPic_X]
document.Willow.src = BuffyArray[RandomPic_W]
document.Spike.src = BuffyArray[RandomPic_S]
document.Dawn.src = BuffyArray[RandomPic_D]
} else {
document.Buffy.src = "images/home/buffy.jpg";
document.Xander.src = "images/home/xander.jpg";
document.Willow.src = "images/home/willow.jpg";
document.Spike.src = "images/home/spike.jpg";
document.Dawn.src = "images/home/dawn.jpg";
}
} // End of Function RandomBuffy
Also I haven't try this yet but I want the function to automaticly change the pics and not on a page reload but I think that setting timers in JavaScript is went to be easy
If it's not asking to much I would like any code pasted for help given explain to some (like me) who doesn't understand JavaScript very well.
Thank You, Glenn Curtis
My Function ::
function RandomBuffy() {
BuffyArray = new Array (
"images/home/buffy.jpg",
"images/home/xander.jpg",
"images/home/willow.jpg",
"images/home/spike.jpg",
"images/home/dawn.jpg");
RandomPic_B = Math.floor(BuffyArray.length * Math.random());
RandomPic_X = Math.floor(BuffyArray.length * Math.random());
RandomPic_W = Math.floor(BuffyArray.length * Math.random());
RandomPic_S = Math.floor(BuffyArray.length * Math.random());
RandomPic_D = Math.floor(BuffyArray.length * Math.random());
if (RandomPic_B != RandomPic_X || RandomPic_W || RandomPic_S || RandomPic_D) {
document.Buffy.src = BuffyArray[RandomPic_B]
document.Xander.src = BuffyArray[RandomPic_X]
document.Willow.src = BuffyArray[RandomPic_W]
document.Spike.src = BuffyArray[RandomPic_S]
document.Dawn.src = BuffyArray[RandomPic_D]
} else {
document.Buffy.src = "images/home/buffy.jpg";
document.Xander.src = "images/home/xander.jpg";
document.Willow.src = "images/home/willow.jpg";
document.Spike.src = "images/home/spike.jpg";
document.Dawn.src = "images/home/dawn.jpg";
}
} // End of Function RandomBuffy