www.webdeveloper.com
Results 1 to 6 of 6

Thread: random link and text

  1. #1
    Join Date
    May 2006
    Posts
    12

    random link and text

    Any ideas on how to create a script that puts a random link and the text that goes with it on a web page?

    I would actualy need more than one synchronised links, something like this:

    <a href="...">text</a>text<a href="...">text</a>text<a link="...">text</a>

    It's really hard to explain this but I want all the "..." links to be one random link out of many. And the text in them has to go with the links. The text in between the links has to be always the same.

  2. #2
    Join Date
    Feb 2006
    Posts
    31
    Hi,

    This should work, not sure if it will show the links but give it a go, well it's a start isnt it?

    <script type="text/javascript">
    //<![CDATA[

    var totalquotes = 3
    var quote = new Array( totalquotes+1)
    quote[0]="text<a href="">text</a>"
    quote[1]="text<a href="">text</a>"
    quote[2]="text<a href="">text</a>"
    quote[3]="text<a href="">text</a>"

    function Random(){
    var i = -1
    while (i < 0 || i > totalquotes || isNaN(i)){
    i = parseInt(Math.random()*( totalquotes+1))
    }
    return i
    }
    quo = Random()
    quox = quote[quo]
    document.write(quox)
    //]]>
    </script>

    ---
    I am lost... in all this code??? :/

  3. #3
    Join Date
    May 2006
    Posts
    12
    How is it possible that i < 0 and i > totalquotes if totalquotes = 3?

  4. #4
    Join Date
    May 2006
    Posts
    12
    That was really helpful. I was able to make my own function that does this:



    function num_random()
    {
    var num= Math.floor(Math.random()*5);
    return num;
    }

    function randomlinks()
    {
    var quoteNumber=num_random();

    var quote=new Array(5)
    quote[0]="static text<a href='http://www.google.com/'>link text1</a>static text<a href='http://www.google.com/'>link text2</a>";
    quote[1]="static text<a href='http://www.newgrounds.com/'>link text3</a>static text<a href='http://www.newgrounds.com/'>link text4</a>";
    quote[2]="static text<a href='http://www.webdeveloper.com/'>link text5</a>static text<a href='http://www.webdeveloper.com/'>link text6</a>";
    quote[3]="static text<a href='http://www.weebls-stuff.com/'>link text7</a>static text<a href='http://www.weebls-stuff.com/'>link text8</a>";
    quote[4]="static text<a href='http://www.amazon.com/'>link text9</a>static text<a href='http://www.amazon.com/'>link text10</a>";

    var outputText=quote[quoteNumber]
    document.write(outputText);
    }




    There's still a problem, whenever I try to embed document.write(outputText); into the body of the document it doesn't work.
    So it keeps writing only the random text and erasing everything else on the site.

  5. #5
    Join Date
    Jul 2004
    Location
    web develepment land
    Posts
    164
    Try caling the document.write() before the document loads

    That should fix it.

    Edit:
    I made some code that works:

    Code:
    links=Array()
    links[0]="text1 <a href='test.html'>Link 1</a> text <a href='test.html'>Link 2</a> text <a href='test.html'>Link 3</a>"
    links[1]="text1 <a href='test.html'>Link 4</a> text <a href='test.html'>Link 5</a> text <a href='test.html'>Link 6</a>"
    links[2]="text <a href='test.html'>Link 7</a> text <a href='test.html'>Link 8</a> text <a href='test.html'>Link 9</a>"
    links[3]="text <a href='test.html'>Link 10</a> text <a href='test.html'>Link 11</a> text <a href='test.html'>Link 12</a>"
    links[4]="text <a href='test.html'>Link 13</a> text <a href='test.html'>Link 14</a> text <a href='test.html'>Link 15</a>"
    function rand() {
    return Math.round(Math.random()*(links.length-1))
    }
    document.write(links[rand()])
    Last edited by emblem; 06-03-2006 at 02:23 PM.
    Need help with any of my scripts? Come chat with me!
    Javascript And File System!
    EMBLEM
    |-____/\____---------------|
    |-\__/||\__/--TRUST--------|
    |--\/|--|\/----THE---------|
    |--/______\-----EMBLEM-----|
    |-----\/-------------------|

  6. #6
    Join Date
    May 2006
    Posts
    12

    Thumbs up

    Yeah, i guess that's simpler.
    Ok it works now. Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles