Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-13-2004, 09:05 PM   PM User | #1
junip
New to the CF scene

 
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
junip is an unknown quantity at this point
Multiple Random Image Generator problem

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 .
Code:
<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:

Code:
<!-- 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:

Code:
<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
junip is offline   Reply With Quote
Old 12-14-2004, 12:03 AM   PM User | #2
Bobo
Regular Coder

 
Join Date: Jan 2004
Location: Port Huron, MI, U.S.A.
Posts: 280
Thanks: 0
Thanked 0 Times in 0 Posts
Bobo is an unknown quantity at this point
Quote:
Originally Posted by junip


Code:
<!-- 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]
__________________
Oh, was I supposed to put something here? ........
Bobo is offline   Reply With Quote
Old 12-14-2004, 12:37 AM   PM User | #3
junip
New to the CF scene

 
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
junip is an unknown quantity at this point
Whoops! I didn't see that...

Tried it and it still didn't work. Now I'm using a different script from willy 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!
junip is offline   Reply With Quote
Old 12-14-2004, 01:22 AM   PM User | #4
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Quote:
Originally Posted by junip
Whoops! I didn't see that...

Tried it and it still didn't work. Now I'm using a different script from willy 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
Willy Duitt is offline   Reply With Quote
Old 12-14-2004, 01:40 AM   PM User | #5
junip
New to the CF scene

 
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
junip is an unknown quantity at this point
Quote:
Originally Posted by Willy Duitt
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.

Thanks anyway.

Tim
junip is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.