...

View Full Version : Multiple Random Image Generator problem



junip
12-13-2004, 09:05 PM
I found a nice script I'd like to use on a client's site (pasted below) The idea is to generate three random images on the home page that are linked to various site pages. This one allows me to define separate images for each of the three arrays. A link to the working script is here (http://www.javascriptworld.com/js4e/scripts/chap03/script16.html) .


<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
myPix1 = new Array("images/pathfinder.gif","images/surveyor.gif","images/surveyor98.gif")
myPix2 = new Array("images/callisto.jpg","images/europa.jpg","images/io.jpg","images/ganymede.jpg")

function choosePix() {
if (document.images) {
randomNum = Math.floor((Math.random() * myPix1.length))
document.myPicture1.src = myPix1[randomNum]

randomNum = Math.floor((Math.random() * myPix2.length))
document.myPicture2.src = myPix2[randomNum]
}
}

// End hiding script from old browsers -->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="WHITE" onLoad="choosePix()">
<IMG SRC="images/spacer.gif" WIDTH="201" HEIGHT="155" NAME="myPicture1" ALT="some image"><br><br>
<IMG SRC="images/spacer.gif" WIDTH="262" HEIGHT="262" NAME="myPicture2" ALT="some image">


However, I can't seem to get the thing to work on my site. The only thing I did was add another array and plug in my images. Only the transparent "space.gif" is loading. Here's my .js file:


<!-- Hide script from old browsers
myPix1 = new Array("image/hollandth.jpg","image/nationalth.jpg","image/brennanth.jpg")
myPix2 = new Array("image/nationalth.jpg","image/brennanth.jpg","image/hollandth.jpg")
myPix3 = new Array("image/brennanth.jpg","image/hollandth.jpg","image/nationalth.jpg")

function choosePix() {
if (document.images) {
randomNum = Math.floor((Math.random() * myPix1.length))
document.myPicture1.src = myPix1[randomNum]

randomNum = Math.floor((Math.random() * myPix2.length))
document.myPicture2.src = myPix2[randomNum]

randomNum = Math.floor((Math.random() * myPix3.length))
document.myPicture3.src = myPix3.randomNum]
}
}
// End hiding script from old browsers -->

And here's what's in the html:


<td><IMG SRC="image/space.gif" WIDTH="158" NAME="myPicture1" ALT="some image"></td>
<td><IMG SRC="image/space.gif" WIDTH="158" NAME="myPicture2" ALT="some image"></td>
<td><IMG SRC="image/space.gif" WIDTH="158" NAME="myPicture3" ALT="some image"></td>

I don't understand why the example code works just fine, yet my nearly identical code doesn't. Any ideas? Sharper eyes?

Thanks in advance,
Tim

Bobo
12-14-2004, 12:03 AM
<!-- Hide script from old browsers
myPix1 = new Array("image/hollandth.jpg","image/nationalth.jpg","image/brennanth.jpg")
myPix2 = new Array("image/nationalth.jpg","image/brennanth.jpg","image/hollandth.jpg")
myPix3 = new Array("image/brennanth.jpg","image/hollandth.jpg","image/nationalth.jpg")

function choosePix() {
if (document.images) {
randomNum = Math.floor((Math.random() * myPix1.length))
document.myPicture1.src = myPix1[randomNum]

randomNum = Math.floor((Math.random() * myPix2.length))
document.myPicture2.src = myPix2[randomNum]

randomNum = Math.floor((Math.random() * myPix3.length))
document.myPicture3.src = myPix3.randomNum]
}
}
// End hiding script from old browsers -->


Change the part I put in red to myPix3.randomNum or myPix3[randomNum] instead of myPix3.randomNum]

junip
12-14-2004, 12:37 AM
Whoops! I didn't see that...

Tried it and it still didn't work. Now I'm using a different script from willy (http://www.codingforums.com/showthread.php?t=43843) and its working. Only that one won't seem to work with IE. I'm getting a little frustrated now, but at least my css is validated!

Thanks!

Willy Duitt
12-14-2004, 01:22 AM
Whoops! I didn't see that...

Tried it and it still didn't work. Now I'm using a different script from willy (http://www.codingforums.com/showthread.php?t=43843) and its working. Only that one won't seem to work with IE. I'm getting a little frustrated now, but at least my css is validated!

Thanks!

I wrote that script using IE6 so there is no reason why it shouldn't work unless you are using an older version of IE which does not support the javascript array push() method...

.....Willy

junip
12-14-2004, 01:40 AM
I wrote that script using IE6 so there is no reason why it shouldn't work unless you are using an older version of IE which does not support the javascript array push() method...

.....Willy

I think that is pretty much exactly the case. Unfortunately, the version I'm using (IE 5.2.3) is the latest version available for mac. I really don't feel like reverse-engineering your script to make it work, so unless you already know how, I'm just going to add a browser detect script or something to send people to a basic html version of the page. :mad:

Thanks anyway.

Tim



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum