javascript 计算访问 url 的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14495228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:47:26  来源:igfitidea点击:

count number of times a url is visited

javascriptjqueryhtml

提问by Nandan Chaturvedi

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.

计算访问 URL 的次数也可以。

回答by Praveen Kumar Purushothaman

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 等编程语言来存储和修改计数。如果你有一个 PHP 服务器,你可以使用一个简单的计数器:

<?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 CodeWell, the name says it all, you can give the counter an Initial start count of your counter !!.
  • ewebsitecounterhas 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.
  • Hit Counter HTML 代码好吧,这个名字说明了一切,你可以给计数器一个你的计数器的初始开始计数!!。
  • 在所有免费的 tumblr 热门计数器中,ewebsitecounter拥有最精美的图标。您可以从许多不同的布局中进行选择,包括动物和季节性图片。
  • Hit-Counter,绝对是那里最受欢迎的 tumblr 在线计数器。输入您博客的详细信息时请小心,因为该网站充满了广告。
  • 在线用户计数器
  • 超级计数器 - 免费点击计数器,用户在线计数器标记计数器网站博客和 tumblr 的访客地图
  • HitWebCounter- 使用最广泛的免费互联网计数器。

回答by Adil

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

您可以使用谷歌分析工具获取此类信息,您可以在此处找到更多信息或者,您可以将逻辑放在服务器端,以计算网站中每个 url 获得的点击量。

回答by Adil

You can user cookies, similar to this one.

您可以使用与此类似的 cookie。

$(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>

回答by whitneyit

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:

我个人会使用服务器端代码或分析来实现这一点,但由于您已标记为 HTML5/JavaScript 问题,因此您可以执行以下操作:

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

更多信息在这里http://www.html5rocks.com/en/features/storage