470,811 Members | 1,268 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

image randomizer not working

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[i] = new Image()
preBuffer[i].src = theImages[i]
}
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?
Jul 20 '05 #1
4 1443
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[i] = new Image()
preBuffer[i].src = theImages[i]
}
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?


Jul 20 '05 #2
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" <RE**************@cox.net> wrote in message
news:bu*********@drn.newsguy.com...
matt said:

I have an image randomizer that was working fine. I added five images to ittoday, 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[i] = new Image()
preBuffer[i].src = theImages[i]
}
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 anyonetell what's wrong?

Jul 20 '05 #3
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" <ma********@hotmail.com> wrote in message
news:Mf********************@comcast.com...
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" <RE**************@cox.net> wrote in message
news:bu*********@drn.newsguy.com...
matt said:

I have an image randomizer that was working fine. I added five images
to
ittoday, 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[i] = new Image()
preBuffer[i].src = theImages[i]
}
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 anyonetell what's wrong?


Jul 20 '05 #4
JRS: In article <8pGOb.167330$ts4.162314@pd7tw3no>, seen in
news:comp.lang.javascript, Simon Wigzell <si**********@shaw.ca> posted
at Mon, 19 Jan 2004 01:28:36 :-
Could this be a caching problem?
SW: Please trim your quotes, and put responses after, as per the FAQ and
Usenet standards.
"matt" <ma********@hotmail.com> wrote in message
news:Mf********************@comcast.com...
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jez | last post: by
9 posts views Thread by Karl Burrows | last post: by
15 posts views Thread by Anand Ganesh | last post: by
reply views Thread by wschlichtman | last post: by
6 posts views Thread by ipy2006 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.