[英]How to find out user clicked 3 times through my website with Javascript
Is there any way in JavaScript how to find out user clicked through the same domain 2 or more times? JavaScript中有什么方法可以找出通过相同域两次或更多次点击的用户?
I need to popup a window after user clicked anywhere on the site for 3 times. 用户单击网站上的任意位置3次后,我需要弹出一个窗口。 I know how to do it after one click - with
document.referrer
or addEventListener
, but then I'm lost. 我知道如何一键操作-使用
document.referrer
或addEventListener
,但是我迷路了。
I need something that will capture all click events (not only links) and count them. 我需要一些可以捕获所有点击事件(不仅是链接)并对其进行计数的东西。
Sure. 当然。 You need to store a list of users' click events, either in a cookie, or in a server-side data store.
您需要在Cookie或服务器端数据存储区中存储用户点击事件的列表。 On every recorded click, increment the count by one, and do your thing when the number hits 3.
在每次记录的点击中,将计数加1,然后在数字达到3时执行操作。
Try using session cookies to store state between pages -- they're fast, pretty widely compatible, and will zero out when the browser shuts down, to keep from spamming your users' cookie jars. 尝试使用会话Cookie在页面之间存储状态-它们是快速的,具有广泛的兼容性,并且在浏览器关闭时将为零,以防止向用户的Cookie罐发送垃圾邮件。
I tried this and it worked fine: 我尝试了一下,效果很好:
window.onload = function() {
var clicked = readCookie('popunder');
if(clicked == null) {
clicked = 0;
}
var allLinks = document.getElementsByTagName("a");
for(i=0;i<=allLinks.length;i++) {
allLinks[i].addEventListener("click",countClicks,true);
}
function countClicks() {
if(clicked == 2) {
popunder(); //something to do
} else {
clicked++;
doCookie('popunder', clicked, 1);
alert(clicked);
}
}
function popunder() { alert('thats 3 clicks!'); }
function doCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
} else {
var expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var readName = name + "=";
var cSplit = document.cookie.split(';');
for(var i=0;i < cSplit.length;i++) {
var sc = cSplit[i];
while (sc.charAt(0)==' ') sc = sc.substring(1,sc.length);
if (sc.indexOf(readName) == 0) return sc.substring(readName.length,sc.length);
}
return null;
}
}
Thanks for all your advice. 感谢您的所有建议。 I tried this code.
我尝试了这段代码。 But after the refresh the clicked variable goes to 0 again.
但是刷新后, 单击的变量再次变为0。 I need to save every new value of clicked into cookie (or whatever else), so its number will rise with every click on link on page.
我需要将点击的每个新值都保存到Cookie(或其他任何值)中,以便其每次点击页面上的链接时其数量都会增加。 Is it possible to change value of the variable in cookie this way?
是否可以通过这种方式更改Cookie中变量的值?
window.onload = function(){
window.onload = function(){
var **allLinks** = document.getElementsByTagName("a");
var **clicked** = 0;
**doCookie**('popunder',clicked,1);
for(i=0;i<=allLinks.length;i++){
allLinks[i].addEventListener("click",countClicks,true);
}
function **countClicks**(){
if(clicked == 3){
popunder(); //something to do
}
else{
alert(readCookie('popunder'));
return clicked++;
}
}
function **doCookie**(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function **readCookie**(name) {
var readName = name + "=";
var cSplit = document.cookie.split(';');
for(var i=0;i < cSplit.length;i++) {
var sc = cSplit[i];
while (sc.charAt(0)==' ') sc = sc.substring(1,sc.length);
if (sc.indexOf(readName) == 0) return sc.substring(readName.length,sc.length);
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.