如何使用JavaScript计算我网站上的访问者数量? [英] How can I count the number of the visitors on my website using JavaScript?

查看:72
本文介绍了如何使用JavaScript计算我网站上的访问者数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个计数器,以便在我的HTML代码中集成,当访问者访问我的网页时,该计数器从1到3。

I need a counter to integrate inside my HTML code that counts from one to three when the visitors visited my webpage.

例如,如果第一位访问者访问了我的网页它计数到1,然后下一个访问者访问了它计数到2的页面,第三个访问者计数到3,然后第四个访问者再次从1开始,依此类推。

For example if the first visitor visited my page it count to 1, then the next visitor visited the page it count to 2, and for the 3rd visitor count to 3, then for the 4th visitor again starts from 1 and so on.

推荐答案

您可以使用cookie并简单地更新用户刷新时检测到的cookie计数,或者使用计时器。一旦你有了cookie,你也可以做不同的事情。您还可以利用本地存储。

You could use a cookie and simply update the cookie count detected when the user refreshes, or with a timer. You could also do different things once you have the cookie. You could also take advantage of localstorage.

签出 - http: //samy.pl/evercookie/

<script type="text/javascript" src="evercookie.js"></script>

<script>
var ec = new evercookie(); 

// set a cookie "id" to "12345"
// usage: ec.set(key, value)
ec.set("id", "12345"); 

// retrieve a cookie called "id" (simply)
ec.get("id", function(value) { alert("Cookie value is " + value) }); 

// or use a more advanced callback function for getting our cookie
// the cookie value is the first param
// an object containing the different storage methods
// and returned cookie values is the second parameter
function getCookie(best_candidate, all_candidates)
{
    alert("The retrieved cookie is: " + best_candidate + "\n" +
        "You can see what each storage mechanism returned " +
        "by looping through the all_candidates object.");

    for (var item in all_candidates)
        document.write("Storage mechanism " + item +
            " returned: " + all_candidates[item] + "<br>");
}
ec.get("id", getCookie); 

// we look for "candidates" based off the number of "cookies" that
// come back matching since it's possible for mismatching cookies.
// the best candidate is most likely the correct one
</script> 

这篇关于如何使用JavaScript计算我网站上的访问者数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆