random link+text, can't write in body

Discussion in 'JavaScript' started by V1C1N1TY, Jun 3, 2006.

  1. #1
    I have made a function that generates a random link and some text. I haven't put it on my site yet(so the links and text aren't what they're going to be), but I've already encountered a problem. I can't use document.write to display it in the body section of the document. Right now it's in the function and that causes it to rewrite all the contents of the webpage. Here is the code:

    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 &nbsp; <a href='http://www.google.com/'>link text1</a> &nbsp; static text &nbsp; <a href='http://www.google.com/'>link text2</a>";
    quote[1]="static text &nbsp; <a href='http://www.newgrounds.com/'>link text3</a> &nbsp; static text &nbsp;<a href='http://www.newgrounds.com/'>link text4</a>";
    quote[2]="static text &nbsp; <a href='http://www.webdeveloper.com/'>link text5</a> &nbsp; static text &nbsp; <a href='http://www.webdeveloper.com/'>link text6</a>";
    quote[3]="static text &nbsp; <a href='http://www.weebls-stuff.com/'>link text7</a> &nbsp; static text &nbsp; <a href='http://www.weebls-stuff.com/'>link text8</a>";
    quote[4]="static text &nbsp; <a href='http://www.amazon.com/'>link text9</a> &nbsp; static text &nbsp; <a href='http://www.amazon.com/'>link text10</a>";

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


    The document.write statement works right now, erasing everything else. I would like to move it to <body>. I dunno why this doesn't work, it just stays blank.
     
    V1C1N1TY, Jun 3, 2006 IP
  2. V1C1N1TY

    V1C1N1TY Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Already solved this problem on another forum;)
     
    V1C1N1TY, Jun 3, 2006 IP
  3. mykoleary

    mykoleary Peon

    Messages:
    64
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    For the benefit of those who may be looking for the same answer, could you please post the solution you came up with?
     
    mykoleary, Jun 3, 2006 IP
  4. V1C1N1TY

    V1C1N1TY Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Somebody proposed a simpler script and it worked. I still don't know why :p
    Here it is:

    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()])
     
    V1C1N1TY, Jun 4, 2006 IP