Results 1 to 5 of 5

Thread: What can I do to make this script work??????

  1. #1
    Join Date
    Jul 2005
    Location
    Kuwait-I'm American
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy What can I do to make this script work??????

    Code:
    <form name="random_link"> //this is wrong I think.
    <input type=text size=32 width=32 name="textbox1" style="text-align=center"> //is that all right?
    </form>
    <script type="text/javascript">
    random_link = new Object(); //is this correct? new Object()?
    var r=Math.random()
    if (r>5) 
    {
    document.write("<a href='home.html'>Home</a>") //example
    }
    else
    {
    document.write("<a href='index.html'>Index</a>") //example
    }
    document.random_link.textbox1.value = //this is where I'm stuck
    </script>
    
    
    </td>
    	<td colspan=2 height=24></td>
    </tr>
    <tr valign="top">
    	<td colspan=4 height=227></td>
    </tr>
    </table> //what does all that mean?

    Do I have aannyytthhiinngg right in there?
    //\\ //\\// || //\\//\\ //\\ ||_
    SOFTWARE

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ungh.

    Math.random()

    is never greater than 5. It is always >=0 but, <1, so to get what you are looking for, by way of a test try:

    r>.5

    Never use javascript style comments in html, they appear as regular text.

    You don't need a form or text input for this script. You don't need to create a new Object() either. This is all you need, it goes in the body of the page, where you want the random link to appear:
    Code:
    <script type="text/javascript">
    var r=Math.random()
    if (r>.5) 
    document.write("<a href='home.html'>Home</a>")
    else
    document.write("<a href='index.html'>Index</a>")
    </script>
    With only two links to choose from, often you will get the same one over and over. You actually have a good idea that is discernible in this script.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2005
    Location
    Kuwait-I'm American
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    but i would like it to be in a box.

    --if you have, time is there a way to make it so it's a random html file?

    like if (r<1) <html src="blah.html">?
    //\\ //\\// || //\\//\\ //\\ ||_
    SOFTWARE

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    What you are asking for is not precisely possible. You cannot put an html file inside of a text input (well, you can but, the result would not be pretty). So, the question becomes what effect are you trying to achieve? Forget about technical terms for the moment. What do you want the page to look like and what (if anything) do you want to have happen when it loads? What to you want to have happen as the user interacts with it?

    Just for kicks I messed around with this idea a bit sometime yesterday. This is not meant to be a solution for you, just something to check out and play with if you like:
    Code:
    <html>
    <head>
    <title>Random Link Rotator</title>
    <script type="text/javascript">
    function randLink(){
    var blip, rlink, r=Math.random()*100
    if (r>=25&&r<50) 
    rlink='<a href="home.html">Home</a>'
    else if (r>=50&&r<75) 
    rlink='<a href="downloads.html">Downloads</a>'
    else if (r>=75)
    rlink='<a href="sitemap.html">Site Map</a>'
    else
    rlink='<a href="index.html">Index</a>'
    crossObj=document.all? document.all.rdiv : document.getElementById('rdiv')
    blip=crossObj.innerHTML.toString()
    blip=blip.substr(blip.lastIndexOf('"'))
    if (rlink.toLowerCase().substr(rlink.lastIndexOf('"'))!==blip.toLowerCase())
    crossObj.innerHTML=rlink
    else
    randLink()
    }
    </script>
    </head>
    <body>
    <div id="rdiv"><a href="index.html">Index</a></div><br>
    <span>Click Below to Generate a Random Link</span><br>
    <input type="button" value="Random Link" onclick="randLink()">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2005
    Location
    Kuwait-I'm American
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    that is exactly what I neeed. Instead of putting html tags and javascript tags, I could just save it as a .js, and:
    Code:
    <form input="<script type="text/javascript" src="random_link_rotator.js">">
    </form>
    //\\ //\\// || //\\//\\ //\\ ||_
    SOFTWARE

Bookmarks

Posting Permissions

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