views:

300

answers:

5

Is it possible to call some JS when the user closes a window?

I searched for JS event handlers and found only onunload, which calls the script whenever the user leaves a page, not necessarily closing the window.

The intended use is to call an AJAX script that unsets a few sessions pertinent to the popup window. There may or may not be a better way of achieving this aim?

I found this DevShed thread in which it is stated that this is possible through JS, but it does not explain exactly how to call the script.

Unfortunately most Google searches for a solution to this bring up window.close, which is effectively the opposite to what I need!

+1  A: 

you can check from the parent window if the child window still exists (e.g. check every 100 ms or so) and launch script if not...

dusoft
You'd probably want to check the URl of the popup page as well, to see if they had navigated away from your domain / app.
iAn
A: 

Have a look at the window.opener property. Except for IE it has pretty good support. You could use it to call some javascript functions in your parent window before window.close()ing your popup.

Josef
I'm not window.clos()ing my popup, I just need to handle the situation where the user clicks the cross!
bcmcfc
ah, but then you should be able to use the unload event, no?
Josef
unload seems to be triggered when the user navigates from page to page within the popup, rather than when they close the window. So it'd cause more issues than it would solve unfortunately.
bcmcfc
A: 

No idea whether it works in practice, but if the issue is navigating to internal pages, why not set a global variable of some type whenever a link is clicked in page, and check for that onunload?

Something like (excuse the jQuery, I'm extremely lazy):

INTERNAL_CLICKED = false;

$("a").click(function() {
    INTERNAL_CLICKED = true;
});

window.onunload = function() {
    if(!INTERNAL_CLICKED) {
     // perform window unload code
    }
}

To reiterate, completely untested, but who knows - it may well just work for you. Although, I would beware, that's probably going to fire on refresh as well.

Simon Scarfe
A: 

Hi You can use following function on onbeforeunload event fired by window just before the window.close

window.onbeforeunload = function () { // stuff do do before the window is unloaded here. }

let me know if it not solve ur probelm

rty
+1  A: 

The following code works in Firefox, IE 8, and "Google Chrome".

In the the opening window

<script src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.3/mootools-yui-compressed.js"&gt;&lt;/script&gt;
<script>
function on_popup_close(){
  //put your code here
  alert('it closed');
}
</script>

In the popup

<script>
function inform_parent(){
  opener.on_popup_close();
} 

window.onbeforeunload = inform_parent;
</script>

The first line in the code for the opening window can be your favorite framework that implements the dollar sign operator.

See my demo here

james.c.funk
Appreciate the demo script you've created, but it still suffers from the original issue- navigating between pages within the popup informs the parent that the popup has closed, when this isn't necessarily the case.
bcmcfc