Click to See Complete Forum and Search --> : random picture on load
esthera
10-26-2003, 02:53 AM
I have a random picture that I load in the body onload statement.
The problem is it causes the page to take a long time to load. Is their anyway to change this to load the random picture after the page loads.
The page is at http://www.nevey.org/home.asp
snoopy0877
10-26-2003, 07:39 PM
First: you store these image in one array. ex:
imgArray[0]='pic01.jpg'
imgArray[1]='pic02.jpg'
imgArray[2]='pic03.jpg'
imgArray[3]='pic03.jpg'
Second: in the random function you call, you limit the number return in the index of imgArray.
goodluck!!!
esthera
10-26-2003, 10:59 PM
I believe that is what I am doing. Here's the code:
var image_index = 0;
image_list = new Array();
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/19.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/3.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/8.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/1.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/17.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/12.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/9.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/2.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/20.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/7.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/15.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/6.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/13.jpg");
image_list[image_index++] = new imageItem("http://www.nevey.org/imagesnew/14.jpg");
var number_of_image = image_list.length;
function imageItem(image_location) {
this.image_item = new Image();
this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) {
return(imageObj.image_item.src)
}
function generate(x, y) {
var range = y - x + 1;
return Math.floor(Math.random() * range) + x;
}
function getNextImage() {
if (random_display) {
image_index = generate(0, number_of_image-1);
}
else {
image_index = (image_index+1) % number_of_image;
}
var new_image = get_ImageItemLocation(image_list[image_index]);
return(new_image);
}
function rotateImage(place) {
var new_image = getNextImage();
document[place].src = new_image;
var recur_call = "rotateImage('"+place+"')";
setTimeout(recur_call, interval);
}
// End -->
</script>
How would I change it?
snoopy0877
10-27-2003, 01:02 AM
var n = 3; //Initialize number of banners
imgsrc = new createArray(n,'');
imgsrc[0] = 'images/pi1.gif';
imgsrc[1] = 'images/pi2.gif';
imgsrc[2] = 'images/pi3.gif';
var i = random(n-1);
document.writeln('<IMG SRC="'+ imgsrc[i] + border="0">');
Your code so complex. I have this code hope it use right for you
pelegk1
10-27-2003, 04:43 AM
your page loads for long time beacuse what u did
u have preloaded all images and beacuse of that the page is being loaded fro long(more correctly until allimages one loading!!!!!
beacuse of that hold the url as a variable in the array instead as animage object (which d/l the image itself!)
for (i=1;i<10;++i)
image_list[i]="http://..............."
and on the onLoad just do
onLoad="addRandomPic()"
function addRandomPic(){
imgObj.src=image_list[random[image_list.length]];
}
pelegk1
10-27-2003, 04:46 AM
and does the picture numbers are for example
from 1 to 20?
then the most simple way is:
<img src='pic<% response.write random(20) %>'></img>