Do JavaScript after window.close()
I needed to do some routines when a user has closed the window. I do not want those routines to trigger when refreshing the page or unloading the document; I just need them to happen when the browser window is closed.
This is the usual proposed solution: assign a function on the
onbeforeunload event of the body or window. The problem with this is it also triggers when refreshing or moving away from the page (i.e. back or forward). I stumbled upon a solution that proposes tapping the
onunload event instead. Thus, I have a function like this:
function body_unload() {
if (window.event.clientX 0) {
alert("The user is closing the window.");
} else {
alert("The user is refreshing or navigating away.");
}
}
I then assign this to the
onunload event of the body. From there I could perform my JavaScript and/or AJAX routines.
Although this works for my purposes, the main downside of this is that I cannot issue a confirm dialog. The page has been unloaded at this point.
Read the complete post at http://alexrazon.blogspot.com/2007/01/do-javascript-after-windowclose.html