Oscar
Oscar

Reputation: 1035

How to know when a popup window closes

I have created a window like so:

var myWindow = window.open(...)

I'm wanting to know when this window closes back in the main code. Something that maybe I can do with the myWindow variable. I've tried using the unonload function such as:

myWindow.onunload = function () {...}

But this isn't being called at all.

Any suggestions?

Upvotes: 2

Views: 117

Answers (2)

jbl
jbl

Reputation: 15413

Not really event handling, but you may try to monitor your window closed property with setInterval :

myWindow=window.open("","","width=200,height=300");

var timer = setInterval(
                    function(){
                      if(myWindow.closed){
                          clearInterval(timer);
                          alert('closed');
                       }
                     }, 1000);

Upvotes: 0

jbabey
jbabey

Reputation: 46647

Instead of adding an event handler from the parent window for when the child closes, you could have the child call a function on the parent:

parent window

// needs to be somewhere global so the child window can call it
var onChildWindowClose = function () {
    // ...
};

child window

window.onunload = function () {
    if (opener && !opener.closed) {
        opener.onChildWindowClose();
    }
};

Upvotes: 3

Related Questions