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 t
onunload
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.
onbeforeunload
Alternatively, you can send out an Ajax request when he leaves.