Can I count clicks on links with javascript?
You can count clicks within a single request (for example, how many times a button was clicked after the page loaded). You cannot count clicks across requests (after you load another page or reload the current page).
Example:
<script type="text/javascript">var clicks = 0;</script>
<input value="Click" type="button" onclick="clicks++">
UPDATE:
You can also use the following (using jQuery) to persist it using cookies as recommended by others:
onclick="$.cookie('clicks', $.cookie('clicks') + 1);"
Sure, add an onclick event handler function to the <a>
tag that retrieves, increments and stores a counter variable. You can retrieve and store this in a hidden field. You will lose the information once you navigate away from the page, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With