View Full Version : script for randomizing images

01-02-2007, 04:36 PM
hi all. i was wondering if there was a way to code in javascript an image to be randomly alternated between other images. for instance, http://www.lscubed.com is my personal site. I would like the "intro" page's image to be alternated between three other images (same design, different color) everytime a visitor goes to that page. kinda like having an image randomly chosen everytime a visitor views the page. any suggestions would be great!!


01-02-2007, 05:03 PM
var imageArray = new Array();
imageArray[0] = path of image 1;
imageArray[1] = path of image 2;
imageArray[2] = path of image 3;

var randomIndex = Math.floor(Math.random()*3);
document.body.style.background = 'url(imageArray[randomIndex])';

01-02-2007, 06:45 PM
suppose u have 3 images with names image_01.jpg, image_02.jpg and image_02.jpg
and the ID of image control in which u want to display images randomly is imgRandom now write javascript on ur page as:

<script language="javascript" type="text/javascript">
var img = '1';

function change_image()
var img = document.getElementById('imgRandom');
if (img == '1')
img = '2';
img.src = 'image_01.jpg';
else if if (img == '1')
img = '3';
img.src = 'image_02.jpg';
else if (img == '3')
img = '1';
img.src = 'image_03.jpg';

setTimeout("change_image()", 10000);




Hope this wud solve ur problem

01-03-2007, 08:02 AM
rock on!!! thanx bro....i'm gonna try this out tonight.