views:

320

answers:

3

Is there any way in JavaScript how to find out user clicked through the same domain 2 or more times?

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.

Thanks so much

+2  A: 

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.

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.

Anirvan
+1 for cookies. I'm using them too to store clientside-only data that has to be persisted across requests.
Vilx-
The problem with cookies for session data is that users can manipulate them. (e.g. with firecookie). Depending on how important it is to remove this possibility, you may be better off using a server side session store.
DanSingerman
Cookies and Javascript can also be disabled, of course. I like your idea, though, Anirvan. +1 from me.
strager
*ANYTHING* on the client can be manipulated with a number of tools, not just cookies. You have to count the clicks in Javascript, so you might as well store them in a cookie.
Jason Jackson
A: 

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?

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;
}
perfectDay
+1  A: 

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;
    }
}
Paolo Bergantino