image randomizer not working

M

matt

I have an image randomizer that was working fine. I added five images to it
today, and none of the new five are ever called. Here's the script:

<!-- Begin

var theImages = new Array()

theImages[0] = '../pros_stu_pics/prospective2.jpg'
theImages[1] = '../pros_stu_pics/23x.jpg'
theImages[2] = '../pros_stu_pics/28xx.jpg'
theImages[3] = '../pros_stu_pics/15x.jpg'
theImages[4] = '../pros_stu_pics/16x.jpg'
theImages[5] = '../pros_stu_pics/24x.jpg'
theImages[6] = '../pros_stu_pics/gral_info.jpg'
theImages[7] = '../pros_stu_pics/admit_1.jpg'
theImages[8] = '../pros_stu_pics/admit_2.jpg'
theImages[9] = '../pros_stu_pics/admit_3.jpg'
theImages[10] = '../pros_stu_pics/admit_4.jpg'
theImages[11] = '../pros_stu_pics/admit_5.jpg'

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

// End -->

and the call script:

<!-- Begin
showImage();
// End -->

All of the new files have been uploaded to the right directory. Can anyone
tell what's wrong?
 
L

Lee

matt said:
I have an image randomizer that was working fine. I added five images to it
today, and none of the new five are ever called. Here's the script:

What do you mean by "are never called"? Do you just always get the
other pictures, or do you get broken images part of the time?

The random function you're using will select the first and last
choice only half as often as the others. It should be:

var whichImage = Math.floor(Math.random()*p);

Check the case of the image pathnames.
"admit1.jpg" would not necessarily be the same as "admit1.JPG"

Try hard-coding the URLs of your last few images and see if
the server can find them.

<!-- Begin

var theImages = new Array()

theImages[0] = '../pros_stu_pics/prospective2.jpg'
theImages[1] = '../pros_stu_pics/23x.jpg'
theImages[2] = '../pros_stu_pics/28xx.jpg'
theImages[3] = '../pros_stu_pics/15x.jpg'
theImages[4] = '../pros_stu_pics/16x.jpg'
theImages[5] = '../pros_stu_pics/24x.jpg'
theImages[6] = '../pros_stu_pics/gral_info.jpg'
theImages[7] = '../pros_stu_pics/admit_1.jpg'
theImages[8] = '../pros_stu_pics/admit_2.jpg'
theImages[9] = '../pros_stu_pics/admit_3.jpg'
theImages[10] = '../pros_stu_pics/admit_4.jpg'
theImages[11] = '../pros_stu_pics/admit_5.jpg'

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

// End -->

and the call script:

<!-- Begin
showImage();
// End -->

All of the new files have been uploaded to the right directory. Can anyone
tell what's wrong?
 
M

matt

thanks. i changed the function and it's still not working. by "not calling",
i mean it just uses the original images. my file names and paths are
correct. in the opinion of this novice, something is telling it that the
array is only 7 images long. or something.




Lee said:
matt said:
I have an image randomizer that was working fine. I added five images to it
today, and none of the new five are ever called. Here's the script:

What do you mean by "are never called"? Do you just always get the
other pictures, or do you get broken images part of the time?

The random function you're using will select the first and last
choice only half as often as the others. It should be:

var whichImage = Math.floor(Math.random()*p);

Check the case of the image pathnames.
"admit1.jpg" would not necessarily be the same as "admit1.JPG"

Try hard-coding the URLs of your last few images and see if
the server can find them.

<!-- Begin

var theImages = new Array()

theImages[0] = '../pros_stu_pics/prospective2.jpg'
theImages[1] = '../pros_stu_pics/23x.jpg'
theImages[2] = '../pros_stu_pics/28xx.jpg'
theImages[3] = '../pros_stu_pics/15x.jpg'
theImages[4] = '../pros_stu_pics/16x.jpg'
theImages[5] = '../pros_stu_pics/24x.jpg'
theImages[6] = '../pros_stu_pics/gral_info.jpg'
theImages[7] = '../pros_stu_pics/admit_1.jpg'
theImages[8] = '../pros_stu_pics/admit_2.jpg'
theImages[9] = '../pros_stu_pics/admit_3.jpg'
theImages[10] = '../pros_stu_pics/admit_4.jpg'
theImages[11] = '../pros_stu_pics/admit_5.jpg'

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

// End -->

and the call script:

<!-- Begin
showImage();
// End -->

All of the new files have been uploaded to the right directory. Can anyone
tell what's wrong?

 
S

Simon Wigzell

Could this be a caching problem? i.e. your browser is still reading the old
version of the page. Try CTRL F5 in the browser to force the latest version
of the page. Put the following in your web page headers to make sure you
always get the latest version of a webpage :

<meta HTTP-EQUIV="Expires" CONTENT="0">

Internet caching is very dumb and lazy, unless explicitly told, it will
gladly give you the version of the page it is sitting on rather than check
and see if there is a newer version!



matt said:
thanks. i changed the function and it's still not working. by "not calling",
i mean it just uses the original images. my file names and paths are
correct. in the opinion of this novice, something is telling it that the
array is only 7 images long. or something.




Lee said:
matt said:
to
it
today, and none of the new five are ever called. Here's the script:

What do you mean by "are never called"? Do you just always get the
other pictures, or do you get broken images part of the time?

The random function you're using will select the first and last
choice only half as often as the others. It should be:

var whichImage = Math.floor(Math.random()*p);

Check the case of the image pathnames.
"admit1.jpg" would not necessarily be the same as "admit1.JPG"

Try hard-coding the URLs of your last few images and see if
the server can find them.

<!-- Begin

var theImages = new Array()

theImages[0] = '../pros_stu_pics/prospective2.jpg'
theImages[1] = '../pros_stu_pics/23x.jpg'
theImages[2] = '../pros_stu_pics/28xx.jpg'
theImages[3] = '../pros_stu_pics/15x.jpg'
theImages[4] = '../pros_stu_pics/16x.jpg'
theImages[5] = '../pros_stu_pics/24x.jpg'
theImages[6] = '../pros_stu_pics/gral_info.jpg'
theImages[7] = '../pros_stu_pics/admit_1.jpg'
theImages[8] = '../pros_stu_pics/admit_2.jpg'
theImages[9] = '../pros_stu_pics/admit_3.jpg'
theImages[10] = '../pros_stu_pics/admit_4.jpg'
theImages[11] = '../pros_stu_pics/admit_5.jpg'

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

// End -->

and the call script:

<!-- Begin
showImage();
// End -->

All of the new files have been uploaded to the right directory. Can anyone
tell what's wrong?


 
D

Dr John Stockton

JRS: In article <8pGOb.167330$ts4.162314@pd7tw3no>, seen in
news:comp.lang.javascript said:
Could this be a caching problem?

SW: Please trim your quotes, and put responses after, as per the FAQ and
Usenet standards.
matt said:
thanks. i changed the function and it's still not working. by "not calling",
i mean it just uses the original images. my file names and paths are
correct. in the opinion of this novice, something is telling it that the
array is only 7 images long. or something.
var theImages = new Array()

theImages[0] = '../pros_stu_pics/prospective2.jpg'
theImages[10] = '../pros_stu_pics/admit_4.jpg'
theImages[11] = '../pros_stu_pics/admit_5.jpg'

All of the new files have been uploaded to the right directory. Can anyone
tell what's wrong?


OP: ISTM that you are testing inefficiently, wasting your own time.

Cease to attempt to fetch the images; instead, display (an alert will
do) the selected names. Whether the names are right or wrong, the
problem is halved.

Don't display the names; display the numbers. Whether the numbers are
right or wrong, the remaining problem is halved.

Don't worry about the alleged length of the image array; display it.

However, apart from the half-probability of the first & last images,
your code selects properly. Therefore, what you posted is not
representative, or you tested wrongly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top