Nandan Chaturvedi
Nandan Chaturvedi

Reputation: 1098

count number of times a url is visited

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

Answers (4)

whitneyit
whitneyit

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

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

Using on your own server

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];
?>

Using a Remote Service

If you don't have the option of hosting a script on your server, you can use some hosted solutions like:

  • Hit Counter HTML Code Well, the name says it all, you can give the counter an Initial start count of your counter !!.
  • ewebsitecounter has the fanciest icons among all tumblr hit counters available for free. You can choose from many different layouts, including animals and seasonal pics.
  • Hit-Counter, definitely the most popular tumblr online counter outthere. Please be careful when entering the details for your blog since the site it’s full of ads.
  • Online Users Counter
  • Supercounters - free hit counter,users online counter flag counter visitor map for website blog and tumblr
  • HitWebCounter - The most widely used Free internet Counter.

Upvotes: 3

user1358445
user1358445

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

Adil
Adil

Reputation: 148150

You can use google analytic tools for such information you can find more over here Alternatively you can put your logic in server side to count the hits each url in your website is getting.

Upvotes: 1

Related Questions