Is there any way in JavaScript how to find out user clicked through the same domain 2 or more times?
在JavaScript中有什么方法可以找出用户点击同一个域2次或更多次?
I need to popup a window after user clicked anywhere on the site for 3 times. I know how to do it after one click - with document.referrer
or addEventListener
, but then I'm lost. I need something that will capture all click events (not only links) and count them.
用户点击网站上任意位置3次后,我需要弹出一个窗口。我知道如何在一次点击后使用document.referrer或addEventListener,但后来我迷路了。我需要能够捕获所有点击事件(不仅仅是链接)并计算它们的东西。
Thanks so much
非常感谢
3 个解决方案
#1
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;
}
}
#2
Sure. You need to store a list of users' click events, either in a cookie, or in a server-side data store. On every recorded click, increment the count by one, and do your thing when the number hits 3.
当然。您需要在cookie或服务器端数据存储中存储用户单击事件的列表。在每次录制的点击中,将计数增加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罐发送垃圾邮件。
#3
Thanks for all your advice. I tried this code. But after the refresh the clicked variable goes to 0 again. 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. Is it possible to change value of the variable in cookie this way?
谢谢你的所有建议。我试过这段代码。但刷新后,单击的变量再次变为0。我需要保存点击进入cookie(或其他任何内容)的每个新值,因此每次点击页面链接时它的数量都会增加。是否可以通过这种方式更改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;
}
#1
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;
}
}
#2
Sure. You need to store a list of users' click events, either in a cookie, or in a server-side data store. On every recorded click, increment the count by one, and do your thing when the number hits 3.
当然。您需要在cookie或服务器端数据存储中存储用户单击事件的列表。在每次录制的点击中,将计数增加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罐发送垃圾邮件。
#3
Thanks for all your advice. I tried this code. But after the refresh the clicked variable goes to 0 again. 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. Is it possible to change value of the variable in cookie this way?
谢谢你的所有建议。我试过这段代码。但刷新后,单击的变量再次变为0。我需要保存点击进入cookie(或其他任何内容)的每个新值,因此每次点击页面链接时它的数量都会增加。是否可以通过这种方式更改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;
}