Rotating links

Discussion in 'HTML & Website Design' started by nickehyz, Aug 9, 2009.

  1. #1
    Is it possible to have one link, randomly choose from 2 pages to load?

    I.E www.domain.com/pages chooses from www.domain.com/pages.html, and www.domain.com/pages2.html
     
    nickehyz, Aug 9, 2009 IP
  2. myst_dg

    myst_dg Active Member

    Messages:
    224
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #2
    This is very simple in JavaScript
    <a href="#" id="link">asdf</a>
    <script>
    var r=Math.random();
    if(r>0.5) {
    	document.getElementById("link").href="www.domain.com/pages.html"
    } else {
    	document.getElementById("link").href="www.domain.com/pages2.html"
    }
    </script>
    Code (markup):
    You may also have other ways to go, depending on what language you are using.
     
    myst_dg, Aug 10, 2009 IP
  3. myst_dg

    myst_dg Active Member

    Messages:
    224
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #3
    A little mistake in understanding what you're talking of, this will now be working
    <script>
    var r=Math.random();
    if(r>0.5) {
    	document.location="www.domain.com/pages.html"
    } else {
    	document.location="www.domain.com/pages2.html"
    }
    </script>
    Code (markup):

    Maybe this is better for visitors won't get fucked if he/she pushes the back button.
    <script>
    var r=Math.random();
    if(r>0.5) {
    	location.replace("www.domain.com/pages.html")
    } else {
    	location.replace("www.domain.com/pages2.html")
    }
    </script>
    Code (markup):
     
    Last edited: Aug 10, 2009
    myst_dg, Aug 10, 2009 IP