Need a script to switch 2 different texts on webpage load

Discussion in 'JavaScript' started by The_Hatta, Feb 4, 2011.

  1. #1
    i need a script that will output - text to a webpage 1/2 the time- then a different text the other half,

    Can anyone help with that, Thanks :)
     
    The_Hatta, Feb 4, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <html>
    <head>
    <script type="text/javascript">
    function randomText() {
    	var x = Math.random();
    	if (x < 0.5) document.getElementById("random").innerHTML = "Text One"
    	else document.getElementById("random").innerHTML = "Text Two";
    }
    </script>
    </head>
    <body onload="randomText()">
    <span id="random"></span>
    </form>
    </body>
    </html>
    
    Code (markup):
    But you often get the same text several times in a row. That's unavoidable unless you have some way of storing the previous text, like with a cookie.
     
    Cash Nebula, Feb 4, 2011 IP