Javascript Click Counter

Discussion in 'JavaScript' started by MrLeN, Mar 8, 2010.

  1. #1
    I have this javascript:

    
    <script type="text/javascript">
    var clicks = 0;
    function linkClick(){
        document.getElementById('clicked').value = ++clicks;
    }
    
    document.write('<a href="#" onclick="linkClick()">Click Me!</a>');
    </script>
    
    Code (markup):
    The above counts clicks.

    
    You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.
    
    Code (markup):
    The above displays the click count.

    Can anyone show me how to make the page redirect to a specified URL after the 10th click?
     
    MrLeN, Mar 8, 2010 IP
  2. BrianM

    BrianM Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <script type="text/javascript">
    var clicks = 0;
    function linkClick(){
        document.getElementById('clicked').value = ++clicks;
    	if (clicks === 10) {
    		window.location = "http://www.google.com/";
    	}
    }
    
    document.write('<a href="#" onclick="linkClick()">Click Me!</a>');
    </script>
    </head>
    Code (markup):
    You're welcome. :)
     
    BrianM, Mar 12, 2010 IP