Skip to content Skip to sidebar Skip to footer

How To Know When Popup Is Closed In Javascript

This code will open go.com in popup window:

Solution 1:

How about instead of directly loading that external page in the window, you open up a "local" window with an iframe containing the external page content? That way you still have enough control over the actual window to be able to tell when it's closed.

Some drawbacks: the address bar will not show the URL they are browsing and it may also break if go.com has some sort of frame busting code.

Some example code:

functionopenIFrameInWindow(url) {
    var myWindow = window.open("");

    var iframe = document.createElement("iframe");
    iframe.src = url;

    myWindow.document.body.appendChild(iframe); 

    myWindow.document.body.onbeforeunload = function () { alert("WINDOW CLOSED") }; 
}

<a href="javascript:void(0)" onClick="javascript:openIFrameInWindow('http://go.com')">open</a>

Solution 2:

How about checking the status of the window every 1 second to see if it has been closed? Sample code, not tested:

var mywindow= window.open();
checkmywindow();

functioncheckmywindow()
{
   if(mywindow.closed)
   {
      //code when popup is closed
   }
   else
   {
      setTimeout("checkmywindow();", 1000);
   }
}

Solution 3:

When the popup window is closed you can INFORM the opener window about it by using the window.opener property.

So:

//pseudocode in pop-up windowonClose() {
 window.opener.popUpClosed = true;
}

Now in your page that opened the popUp you can check for the value of the popUpClosed variable.

More on the opener property here: http://www.webreference.com/js/tutorial1/opener.html

Post a Comment for "How To Know When Popup Is Closed In Javascript"