Javascript to display random page?

Discussion in 'Web Design and Development' started by Nitromaster, Dec 1, 2007.

  1. Nitromaster macrumors 6502

    Joined:
    Jan 7, 2007
    Location:
    Ireland.
    #1
    Bit lost here.....

    I need some javascript that when clicked brings you to an url with a random number at the end,

    Something like
    www.somesite.com/userid= and then the random number.

    I cannot figure out how to do it myself, nor can find any guides on how to do something similar.
    Any guides for random urls ive found require you to make a list of each url.


    Any help?
     
  2. devpro macrumors newbie

    Joined:
    Dec 2, 2007
    #2
    I take it your not a programmer as this is very simple to do. Here is a sample script I made that generates a random number between 0 and 100 for the user id, then simple appends it to the base link.

    <HTML>
    <HEAD></HEAD>

    <BODY>

    <script type="text/javascript">

    function random_link(){

    var random_num=Math.round(Math.random()*101)
    var base_link="http://www.blah.com/userid="
    var targetlink= base_link + random_num;
    window.location=targetlink
    }
    </script>

    <a href="JavaScript:random_link()">Random link</a>

    </BODY>
    </HTML>
     
  3. Nitromaster thread starter macrumors 6502

    Joined:
    Jan 7, 2007
    Location:
    Ireland.
    #3
    Thanks :D


    What if you want it to pick a random number between say 1000 and 2000?
     
  4. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #4
    HTML:
    <script type="text/javascript">
    function randLink()
    {
         var minVal=1000;
         var maxVal=2000;
         var userId=Math.round((Math.random()*(maxVal-minVal))+minVal);
         var link="http://yoursite.com/userid=";
         window.location=link+userId;
    }
    </script>
    
    <a href="javascript:randLink()">Random link</a>
    where minVal is your minimum value, maxVal is your maximum value and link is the website address.
     

Share This Page