Tuesday, January 09, 2007

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.

Labels: , , ,

1 Comments:

At 1/22/2008 5:20 PM , Blogger SathishKumar said...

Hi,
I want to remove the status,address and toolbars for the current window and resize it and move it to the bottom when the page loads.Removing the status,address and toolbars can be done only through window.open it seems.So,I called another page and resized it and removed the status,address and toolbars.Now I dont need the parent window that called the current page.How could I close the parent page without the warning.Thanks for any help ..

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home