Skip to content Skip to sidebar Skip to footer

Is It Possible To Track Visitor That Leave Or Page To Another Sites?

Is it possible to track visitor that leave or page to another sites, like ask question only when they want move to another sites or other domain name? I write this code

Solution 1:

I think this is a task for a browser plugin, not a page script. It's not possible for JS to know what the user does outside the page.

If this were possible, that would be a whole new security problem, a privacy issue, and I won't visit your site.

Solution 2:

As you have tagged jquery in this question I guess you are already using http://jquery.com/

<scriptlanguage="JavaScript">
  $(document).ready(function(){
       $("a").click(function(){
       if( fnGetDomain(document.location) == fnGetDomain($(this).attr("href")) )
       {
            returntrue;
       }
       else{
            returnconfirm("Leave page?");
       }

   });
});
functionfnGetDomain(url) {
  return url.match(/:\/\/(.[^/]+)/)[1];
}
</script>

You can use document.location.href in place of document.location if there is any problem

Solution 3:

I've tried a thing here, not sure it works on every cases...

window.onbeforeunload = confirmExit;

function confirmExit(event)
{
    console.log(event);
    console.log("Current : " + event.srcElement.domain);
    console.log("Target : " + event.target.domain);
    if (event.srcElement.domain != event.target.domain)
    {
        return"leave ?";
    }
    else
    {
        // staying here
    }
}​

Post a Comment for "Is It Possible To Track Visitor That Leave Or Page To Another Sites?"