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 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2005
    Location
    Near some trees
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Load a random page

    The title speaks for itself, I hope somebody can help. I just need to create a link. Instead of the link going to a specific page though it needs to load 1 of 20-30 pages at random.

  • #2
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try asking this in the javascript catergory as this cannot be done without any extra scripting to add the randomisation.
    Mike

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If you use PHP here's a simple script that'll do it for you.

    PHP Code:
    $links = array();
    $links[] = 'http://www.example.com/page1.php';
    $links[] = 'http://www.example.com/page2.php';
    $links[] = 'http://www.example.com/page3.php';
    $links[] = 'http://www.example.com/page4.php';
    //etc.
    $link array_rand($links);
    echo 
    '<a href="' $link '">Random Link</a>'

  • #4
    New Coder
    Join Date
    Oct 2005
    Location
    Near some trees
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, all good info!

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    PHP Code:
    <html><head><title>multi-link</title>
    <
    script type="text/javascript"><!--
    var 
    numLinks 4;
    var 
    links = new Array(numLinks);
    links[0] = "http://www.lycos.com/";
    links[1] = "http://www.yahoo.com/";
    links[2] = "http://www.google.com/";
    links[3] = "http://www.altavista.com/";
    window.onload=function(){
        if(
    typeof(document.getElementById)!="undefined"){
            
    document.getElementById("rndLink").setAttribute("href",links[Math.floor(Math.random()*numLinks)]);
        }
    }
    //--></script>
    </head><body>
    <
    a href="http://www.google.com/" id="rndLink">Random Link</a>
    </
    body>
    </
    html
    This example uses javascript, and degrades gracefully.


  •  

    Posting Permissions

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