我需要一个计数器来集成到我的 HTML 代码中,当访问者访问我的网页时,该计数器从一计数到三。
例如,如果第一个访问者访问我的页面,则计数为 1,那么下一个访问者访问该页面,则计数为 2,对于第三个访问者,计数为 3,然后对于第四个访问者,又从 1 开始,依此类推。
您可以使用 cookie,并简单地更新用户刷新时检测到的 cookie 计数,或者使用计时器。获得 cookie 后,您还可以做不同的事情。您还可以利用本地存储。
查看 -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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)