Best way to detect when a user leaves a web page?
Try the onbeforeunload
event: It is fired just before the page is unloaded. It also allows you to ask back if the user really wants to leave. See the demo onbeforeunload Demo.
Alternatively, you can send out an Ajax request when he leaves.
How to detect when a user leaves the site?
You could use socket.io and listen for when the socket is lost, or you could have your site send a heartbeat to the server and if X milliseconds goes by without a pulse, you can assume the user left for any of the reasons you listed.
Detect when a user leaves a website
Using jquery you can trigger this:
$(window).bind('beforeunload', function() {
// ajax call perhaps
// triggering a write to db or filesystem...
});
Pure javascript way:
<html>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.somewhere.com">Click here to navigate to
www.somewhere.com</a>
</body>
</html>
Proper way to detect user's leaving page
If each of your pages has a webSocket connection to your server, then on the server you can see when any given page is closed by seeing that the webSocket gets closed.
To avoid thinking that a user has left the site when they are just navigating from one page in your site to another, you simply need to add a delay server-side so that you only report that the user has left your site if there has been no webSocket connection from this user for some time period (probably at least a few seconds).
So, on your server when you detect that the last webSocket connection for this user has been closed, you set a timer for some number of seconds. If the user opens up another page on your site (either via navigation or just opens another page) before the timer goes off, you cancel the timer and count the user as still connected. If the timer goes off, then you now know that the user has been away from your site for whatever time period you picked (say 10 seconds) and to you, this will signify that they have left the site. You can control how long you want that time period to be before you decide that, yes they are gone.
All attempts at trying to "see" the user leaving your page IN the browser with Javascript are going to have holes in them because there are always ways for a web page to get closed without your client-side javascript having a chance to tell your server. The beauty of the webSocket solution is that the browser automatically and reliably tells your server when the page is now gone because it closes the webSocket and your server receives the notification that the socket has been closed.
How can I detect when the user is leaving my site, not just going to a different page?
It's not possible to do this 100% reliably, but if you detect when the user has clicked on a link on your page, you could use that as a mostly-correct signal. Something like this:
window.localLinkClicked = false;
$("a").live("click", function() {
var url = $(this).attr("href");
// check if the link is relative or to your domain
if (! /^https?:\/\/./.test(url) || /https?:\/\/yourdomain\.com/.test(url)) {
window.localLinkClicked = true;
}
});
window.onbeforeunload = function() {
if (window.localLinkClicked) {
// do stuff
} else {
// don't
}
}
Related Topics
How to Round to 1 Decimal Place in JavaScript
Using Jquery to Test If an Input Has Focus
If a Dom Element Is Removed, Are Its Listeners Also Removed from Memory
How to Output Numbers With Leading Zeros in JavaScript
Pad a Number With Leading Zeros in JavaScript
How to Post Urlencoded Form Data With $Http Without Jquery
What Happened to Console.Log in IE8
Make Iframe Height Dynamic Based on Content Inside- Jquery/Javascript
Prevent Browser Caching of Ajax Call Result
In JavaScript, How to Conditionally Add a Member to an Object
Creating Object With Dynamic Keys
Surprised That Global Variable Has Undefined Value in JavaScript
Check Whether User Has a Chrome Extension Installed
How to Set a Cookie for Another Domain
Catch Browser's "Zoom" Event in JavaScript
How to Determine If JavaScript Array Contains an Object With an Attribute That Equals a Given Value
Count the Number of Occurrences of a Character in a String in JavaScript