Question on Variables, Arrays, and Multiple Instances

S

seven

This might be a newbie question or otherwise easy stuff for some of
you, but it's tweaking me ... can't seem to get it to work right.

I am working with a script that basically creates two arrays, and with
a single call in the html, randomly inserts a graphic (which is linked
to another page, as defined in the second array). The working example
is
http://seven.postmodern.com/insight/tarot/1cardreading.html

What I would like to do is call multiple instances of random images,
but the way I have it currently set up, I get the same random image if
I try to call it more than once - so I've tried defining additional
variables but perhaps I am doing it incorrectly. Can anyone help or
offer a tip/tutorial on this if possible? I can workaround by using a
frameset but that gets ugly if doing too many cards. help?

var imagenumber = 44 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
images = new Array
images[1] = "image1.gif"
images[2] = "image2.gif"
....
images[44] = "image22r.gif"
var image = images[rand1]

links = new Array
links[1] = "image1.htm"
links[2] = "image2.htm"
....
links[44] = "image22r.htm"
var link = links[rand1]

/then it within the page, calls the graphic as such
<script language="JavaScript">
<!-- Hide this script from old browsers -->

document.write('<A HREF="' + link + '"target="_blank"><IMG SRC="' +
image + '" border="0"></a>')
<!-- End Hiding Here -->
</script>
 
L

Lee

seven said:
<script language="JavaScript">
<!-- Hide this script from old browsers -->

document.write('<A HREF="' + link + '"target="_blank"><IMG SRC="' +
image + '" border="0"></a>')
<!-- End Hiding Here -->
</script>


I love the superstitious "hiding from old browsers", done
in a way that wouldn't hide anything from any browser.

Thanks
 
J

Jim Ley

seven said:



I love the superstitious "hiding from old browsers", done
in a way that wouldn't hide anything from any browser.

but at least it would work in an XHTML browser!

Jim.
 
S

seven

I love the superstitious "hiding from old browsers", done
in a way that wouldn't hide anything from any browser.

I know, holdovers that don't do anything....

Any ideas on the variable control?


---
http://seven.postmodern.com
seven at post modern dot com
If sending an email, +add+ the word
"newsgroup" to the subject line to
temporarily avert the spam filter.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
seven <[email protected]> posted at
Sat, 17 Jan 2004 06:47:36 :-

What I would like to do is call multiple instances of random images,
but the way I have it currently set up, I get the same random image if
I try to call it more than once - so I've tried defining additional
variables but perhaps I am doing it incorrectly. Can anyone help or
offer a tip/tutorial on this if possible? I can workaround by using a
frameset but that gets ugly if doing too many cards. help?

Read the c.l.j FAQ.

If your images repeat, it is probably because the names repeat. Cut out
image-fetching and show the names.

If your names repeat, it is probably because the indexes repeat. Cut
out name look-up and show the indexes.

Always simplify a problem until it becomes obvious where the trouble is.
var imagenumber = 44 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;

Generates unevenly distributed numbers; see c.l.j FAQ.
images = new Array

Your images is the name of an existing part of the document; use a
different identifier such as imgs
images[1] = "image1.gif"
images[2] = "image2.gif"
...
images[44] = "image22r.gif"

It is silly to test with 44 elements when failures occur. Test with,
say, 4.

DSS.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top