Question on Variables, Arrays, and Multiple Instances

Discussion in 'Javascript' started by seven, Jan 17, 2004.

  1. seven

    seven Guest

    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>


    ---
    http://seven.postmodern.com
    seven at post modern dot com
    If sending an email, +add+ the word "newsgroup" to the subject line to temporarily work around my spam block utility.
    seven, Jan 17, 2004
    #1
    1. Advertising

  2. seven

    Lee Guest

    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
    Lee, Jan 17, 2004
    #2
    1. Advertising

  3. seven

    Jim Ley Guest

    On 16 Jan 2004 23:12:56 -0800, Lee <> wrote:

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


    but at least it would work in an XHTML browser!

    Jim.
    --
    comp.lang.javascript FAQ - http://jibbering.com/faq/
    Jim Ley, Jan 17, 2004
    #3
  4. seven

    seven Guest

    On 16 Jan 2004 23:12:56 -0800, Lee <> wrote:

    >
    >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.
    seven, Jan 17, 2004
    #4
  5. JRS: In article <>, seen in
    news:comp.lang.javascript, seven <> 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.



    --
    © 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.
    Dr John Stockton, Jan 17, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  2. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
  3. Replies:
    8
    Views:
    461
    James Stroud
    Jan 29, 2009
  4. Nav
    Replies:
    15
    Views:
    555
    Steven D'Aprano
    Jan 5, 2010
  5. Paul Roche

    confused: instances or arrays?

    Paul Roche, Oct 6, 2010, in forum: Ruby
    Replies:
    2
    Views:
    128
    Robert Klemme
    Oct 6, 2010
Loading...

Share This Page