如何使用 JavaScript 计算我网站上的访问者数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31610985/
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
How can I count the number of the visitors on my website using JavaScript?
提问by gola
I need a counter to integrate inside my HTML code that counts from one to three when the visitors visited my webpage.
我需要一个计数器来集成到我的 HTML 代码中,当访问者访问我的网页时,该计数器从一计数到三。
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.
例如,如果第一个访问者访问我的页面计数为 1,然后下一个访问者访问的页面计数为 2,对于第 3 个访问者计数为 3,然后对于第 4 个访问者再次从 1 开始,依此类推。
回答by Goddard
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.
您可以使用 cookie 并简单地更新用户刷新时检测到的 cookie 计数,或者使用计时器。一旦你有了 cookie,你也可以做不同的事情。您还可以利用本地存储。
Check out - http://samy.pl/evercookie/
退房 - 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>
回答by Anthony Hilyard
As indicated in the comments, if you are just looking for some method of loading random pages with equal probability, without more complicated server-side code, you could do something like this:
正如评论中所指出的,如果您只是在寻找某种以等概率加载随机页面的方法,而无需更复杂的服务器端代码,您可以执行以下操作:
<script type="text/javascript">
window.onload = function() {
var numberOfPages = 3;
var num = Math.round(Math.random() * numberOfPages);
window.location.href = "/" + num + ".html";
};
</script>
Of course, then you would have pages 0.html
, 1.html
, and 2.html
. This will randomly load one of these three pages with equal probability. It can also be extended to as many pages as you wish, just add more pages and increase the numberOfPages
variable. This should work if you put this script within your site's index--the rest of the page can be empty.
当然,那么你将有网页0.html
,1.html
和2.html
。这将以相等的概率随机加载这三个页面之一。它也可以根据需要扩展到任意多的页面,只需添加更多页面并增加numberOfPages
变量即可。如果您将此脚本放在站点的索引中,这应该会起作用——页面的其余部分可以为空。