Reputation: 1098
there are few anchor tags (links on my page). I want to keep a count of how many times each link is visited and display only the ones which are mostly visited.
Is there a way to do that .?
Or
to count how many times a URL is visited is also fine.
Upvotes: 0
Views: 15260
Reputation: 1226
Personally I would use server side code or analytics to acheive this, but since you've tagged as a HTML5/JavaScript question, you could do something along the lines of:
if ( localStorage ) {
jQuery( 'a' ).click( function () {
var urls = localStorage.getItem( 'urls' );
urls = ( urls != null ) ? JSON.parse( urls ) : {};
urls[ this.href ] = urls[ this.href ] || 0;
urls[ this.href ]++;
urls = JSON.stringify( urls );
localStorage.setItem( 'urls', urls );
});
}
More information here http://www.html5rocks.com/en/features/storage
Upvotes: 0
Reputation: 167192
You need a server application, which uses programming language like PHP to store and modify counts. If you have a PHP Server, there's a simple hit counter you can do with this:
<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>
If you don't have the option of hosting a script on your server, you can use some hosted solutions like:
Upvotes: 3
Reputation:
You can user cookies, similar to this one.
$(document).ready(function() {
if (!$.cookie("urlCookie")) {
var url = document.referrer;
var match = url.indexOf("localhost");
if (match != -1) {
$.cookie("urlCookie", url);
$("#title").html(url);
}
} else {
$("#title").html($.cookie("urlCookie"));
}
});
<h1 id="title"></h1>
Upvotes: 0