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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Java Redirect?

    Does anyone have a javascript coding that will randomly redirect a user to one of three links list?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    var arrLinks = ('page1.htm','page2.htm','page3.htm')

    function redir() {
    var rand = Math.floor(Math.random() * arrLinks.length);
    top.location.href=arrLinks[rand];
    }

    <a href="javascript:redir()">Redirect Me!</a>

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how would I make it redirect on page load? would a onload work?

    <script javascript>
    var arrLinks = ('page1.htm','page2.htm','page3.htm')

    function redir() {
    var rand = Math.floor(Math.random() * arrLinks.length);
    top.location.href=arrLinks[rand];
    }
    </script>
    <body onload="redir()">

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure! Should work with any event handler....

  • #5
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how come this isn't working?

    <html>
    <head>
    <title></title>
    </head>
    <script language="JavaScript">
    var arrLinks = ('http://www.codes.com','http://www.yahoo.com','http://www.hotmail.com')

    function redir() {
    var rand = Math.floor(Math.random() * arrLinks.length);
    top.location.href=arrLinks[rand];
    }

    </script>
    <body onload="redir()">

    </body>
    </html>

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops, sorry, in my haste I made 2 mistakes. Here

    var arrLinks = new Array('page1.htm','page2.htm','page3.htm')

    function redir() {
    var rand = Math.ceil(Math.random() * arrLinks.length);
    top.location.href=arrLinks[rand];
    }

  • #7
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the first link intel.com doesn't seem to load. an undefined error occurs

    <html>
    <head>
    <title></title>
    </head>
    <script language="JavaScript">
    var arrLinks = new Array('http://www.intel.com','http://www.yahoo.com','http://www.hotmail.com')

    function redir() {
    var rand = Math.ceil(Math.random() * arrLinks.length);
    top.location.href=arrLinks[rand];
    }


    </script>
    <body onload="redir()">

    </body>
    </html>

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops again. Change Math.ceil back to Math.floor


  •  

    Posting Permissions

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