Click to See Complete Forum and Search --> : multiple random image scripts?


haasboy
07-18-2003, 12:38 PM
It seems that this forum fills up fast, so I am re-posting this in a hopefully clearer form.

I have this script in the head of my HTML

<SCRIPT language=JavaScript>
var theImages = new Array()
theImages[0] = '1.gif'
theImages[1] = '2.gif'
theImages[2] = '3.gif'
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}
</SCRIPT>

And this in the body.

<SCRIPT language=JavaScript>
showImage();
</SCRIPT>

I want to set up a seperate array of images in the head which would populate a seperate area of the page. So I would have multiple random image areas on the page. I don't know how to distinguish them so that 2 scripts in the head will populate 2 distinct regions on the page with random images. Thanks for patience and help.

Exuro
07-18-2003, 01:27 PM
I think you're looking for something like this:

<html>
<head>
<SCRIPT language=JavaScript>
var theImages = new Array()
theImages[0] = '1.gif'
theImages[1] = '2.gif'
theImages[2] = '3.gif'
var otherImages = new Array()
otherImages[0] = 'a.gif'
otherImages[1] = 'b.gif'
otherImages[2] = 'c.gif'
var j = 0
var p = theImages.length;
var preBuffer = new Array()
var preBuffer2 = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
preBuffer2[i] = new Image()
preBuffer2[i].src = otherImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
var otherImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}
function showImage2(){
document.write('<img src="'+otherImages[otherImage]+'">');
}
</SCRIPT>
</head>
<body>

<SCRIPT language=JavaScript>
showImage();
</SCRIPT>
blablabla
<SCRIPT language=JavaScript>
showImage2();
</SCRIPT>
</body>
</html>

haasboy
07-18-2003, 01:30 PM
i believe that is exactly what I'm after. Thanks a lot!

mbockstruck
10-14-2005, 01:29 PM
hi. i am a beginner with javascript right now. the above code works great for having random images showing up in 2 spots on a page. how do i change the code if i want to have random images show up in 4 different spots?