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 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    93
    Thanks
    12
    Thanked 1 Time in 1 Post

    Script to redirect page to a random one

    Hi,
    I am pretty much a noob when it comes to javascript. I am looking for a script that redirects random.html to 1 of 86 predefined pages, randomly. If someone could help me out, that would be great.

    Thanks for your help

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Code:
    <script type = "text/javascript">
    
    function reDirect() {
    x = Math.ceil(Math.random() * 86);
    newPage = "Page" + x + ".html";
    window.location = newPage;
    }
    
    </script>

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    New Coder
    Join Date
    Jun 2008
    Posts
    93
    Thanks
    12
    Thanked 1 Time in 1 Post
    Thanks for the quick response! I just have 2 quick questions. Do I put this code in body or in the head? And, do I need edit this or just copy paste?

    Thanks

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by millsi80 View Post
    Thanks for the quick response! I just have 2 quick questions. Do I put this code in body or in the head? And, do I need edit this or just copy paste?

    Thanks
    In the <head>.

    Well, obviously you must edit the page names to your actual names.
    newPage = "Page" + x + ".html";

    This will redirect to Page45.html etc.
    I have assumed your pages are named Page1.html through Page86.html.

  • #5
    New Coder
    Join Date
    Jun 2008
    Posts
    93
    Thanks
    12
    Thanked 1 Time in 1 Post
    So I put the code in the head part but it doesn't work. I modified it to test with 2 pages
    Code:
    <head>
    <script type = "text/javascript">
    
    function reDirect() {
    x = Math.ceil(Math.random() * 2);
    newPage = "Page" + 1 + ".html";
    newPage = "Page" + 2 + ".html";
    window.location = newPage;
    }
    
    </script>
    </head>
    When I go to random.html, it doesn't redirect

    Please tell me what I did wrong

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Just follow what you have been given:-

    newPage = "Page" + x + ".html";

    You need to call the function:-


    Code:
    <script type = "text/javascript">
    
    function reDirect() {
    x = Math.ceil(Math.random() * 86);  // change 86 to 2 for your initial test
    newPage = "Page" + x + ".html";
    window.location = newPage;
    }
    
    reDirect();
    
    </script>
    Last edited by Philip M; 06-19-2008 at 02:16 PM.

  • Users who have thanked Philip M for this post:

    millsi80 (06-19-2008)

  • #7
    New Coder
    Join Date
    Jun 2008
    Posts
    93
    Thanks
    12
    Thanked 1 Time in 1 Post
    Ok, thanks very much, works perfect!

  • #8
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    This works great..but is there a way to make it so that there is a limit to how many times the random page has been reloaded?

    Let say for example once it randomly redirects to these pages.x.html, y.html, z.html can you give it a limit to how many times the each page has been reloaded? let say each page has been reloaded 5 times already and after that.. either have it timeout or redirect to another page? It sounds simple, but I have limited knowledge with javascript, and cant figure out how to do this. or maybe this needs to be Server-side?

    any help would do..Thanks!

  • #9
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @philip m
    Thnx dude,worked like a charm for me.


  •  

    Posting Permissions

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