Question on Variables, Arrays, and Multiple Instances

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seven

    Question on Variables, Arrays, and Multiple Instances

    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


    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.g if"
    var image = images[rand1]

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

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

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


    ---

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

    #2
    Re: Question on Variables, Arrays, and Multiple Instances

    seven said:
    [color=blue]
    > <script language="JavaS cript">
    ><!-- Hide this script from old browsers -->
    >
    >document.write ('<A HREF="' + link + '"target="_blan k"><IMG SRC="' +
    >image + '" border="0"></a>')
    ><!-- End Hiding Here -->
    ></script>[/color]


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

    Thanks

    Comment

    • Jim Ley

      #3
      Re: Question on Variables, Arrays, and Multiple Instances

      On 16 Jan 2004 23:12:56 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
      [color=blue]
      >seven said:
      >[color=green]
      >> <script language="JavaS cript">
      >><!-- Hide this script from old browsers -->
      >>
      >>document.writ e('<A HREF="' + link + '"target="_blan k"><IMG SRC="' +
      >>image + '" border="0"></a>')
      >><!-- End Hiding Here -->
      >></script>[/color]
      >
      >
      >I love the superstitious "hiding from old browsers", done
      >in a way that wouldn't hide anything from any browser.[/color]

      but at least it would work in an XHTML browser!

      Jim.
      --
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • seven

        #4
        Re: Question on Variables, Arrays, and Multiple Instances

        On 16 Jan 2004 23:12:56 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
        [color=blue]
        >
        >I love the superstitious "hiding from old browsers", done
        >in a way that wouldn't hide anything from any browser.[/color]

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

        Any ideas on the variable control?


        ---

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

        Comment

        • Dr John Stockton

          #5
          Re: Question on Variables, Arrays, and Multiple Instances

          JRS: In article <oolh001psi44d5 1aemjro46j1du37 irj03@4ax.com>, seen in
          news:comp.lang. javascript, seven <nospam@checkth esigline.com> posted at
          Sat, 17 Jan 2004 06:47:36 :-

          [color=blue]
          >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?[/color]

          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.
          [color=blue]
          >var imagenumber = 44 ;
          >var randomnumber = Math.random() ;
          >var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;[/color]

          Generates unevenly distributed numbers; see c.l.j FAQ.
          [color=blue]
          >images = new Array[/color]

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

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

          [color=blue]
          >---[/color]
          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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...