Random Link

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

    #1

    Random Link

    Hey all,
    I'm new to java script, and really don't know anything about it. I'm
    looking to have a random link shown on my site. I want the link to be chosen
    from a list of about 10 links.
    How would I go about this? I found this piece of code, but this only allows
    two links. Does anyone know how to expand on this to add 10 or more?

    <script type="text/javascript">
    var r=Math.random()
    if (r>0.5)
    {
    document.write( "<a href='http://www.w3schools.c om'>Learn Web
    Development!</a>")
    }
    else
    {
    document.write( "<a href='http://www.refsnesdata .no'>Visit Refsnes
    Data!</a>")
    }
    </script>

    Thanks for any help,

    --
    Global_Killa
    "You're a victim of the rules you live by!"


  • Ivo

    #2
    Re: Random Link

    "Global_Kil la" wrote[color=blue]
    > I'm new to java script, and really don't know anything about it. I'm
    > looking to have a random link shown on my site. I want the link to be[/color]
    chosen[color=blue]
    > from a list of about 10 links.
    > How would I go about this? I found this piece of code, but this only[/color]
    allows[color=blue]
    > two links. Does anyone know how to expand on this to add 10 or more?
    >
    > <snip>[/color]

    There are many ways to do this. Close to your original code, but allowing
    for an arbitrary number of links, is this:

    thelinks=['www.yahoo.com/','www.google.c om/','www.c.com/','www.d.com/'];
    thetexts=['Yahoo','Google ','c','d'];
    a=Math.floor(th elinks.length*M ath.random());
    document.write( '<a href="http://'+thelinks[a]+'">'+thetext s[a]+'</a>');

    It should be mostly self-explanatory, if it isn't, let us know.
    HTH
    Ivo



    Comment

    Working...