Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Website Link

    Hello,

    I'm looking for a link to place into my site that, when clicked, redirects the user to a random website in a list of websites that I have.

    Now, I have a CSS code that does exactly that, but it's in the form of a button. I just need a plain text link. I'm not too fluent in CSS.

    Can anyone help me?


    Here's the code that I have currently:

    <script>
    <!--

    var randomlinks=new Array()

    randomlinks[0]="http://freewarejava.com"
    randomlinks[1]="http://javascriptkit.com"
    randomlinks[2]="http://dynamicdrive.com"
    randomlinks[3]="http://cnn.com"
    randomlinks[4]="http://www.geocities.com"

    function randomlink(){
    window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
    }
    //-->
    </script>
    <form>
    <p><input name="B1" type="submit" onclick="randomlink()" value="Random Site" readonly="readonly"></p>
    </form>

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,950
    Thanks
    9
    Thanked 724 Times in 718 Posts
    I'm pretty sure you can't render a form input as a text link, but here a simple link would do the job:

    Code:
    <a href="#" onclick="randomlink()">Random link</a>

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Something like this.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
            <script type="text/javascript">
                var urls = ['url1','url2','url3','url4','url5','url6'];
                window.onload=function(){
                    document.getElementById('lnk1').onclick=function(){
                        this.href = urls[Math.floor(Math.random()*urls.length)];
                    }
                }
            </script>
        </head>
        <body>
            <div>
                <a href="" id="lnk1">Click me</a>
            </div>
        </body>
    </html>
    Last edited by bullant; 04-03-2011 at 08:04 AM.


  •  

    Posting Permissions

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