What is the best way to detect if a user leaves a web page?
The onunload JavaScript event doesn’t work every time (the HTTP request takes longer than the time required to terminate the browser).
Creating one will probably be blocked by current browsers.
Try the
onbeforeunloadevent: 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.