Is it possible for the parent window to notice if the child window is closed?

I have parent window (opener) and child (popup)

 ---------- -------------- | | | | | parent | -----> opens popup | child | | | | | ----------- -------------- 

Let's say on the parent page I have a js hello() function

So that the child calls parent hello () when the child window is closed and also passes an argument, I can do

 window.close(); window.opener.hello(someArgument); 

This will close the window and also call parent hello ();

But what if I don't want to have window.opener.hello () code on the child page? I want to say that I want the code to only be on the parent page

One thing I can think of is:

A few parents know when the child is closed (listenr event ??? not sure about js) But in this case, how to get the argument? (i.e. some data back from the child)

+7
source share
2 answers

The obvious solution (adding the onunload event handler onunload to the popup) will not work in IE. However, using attachEvent works in IE, so the following will be executed:

 var win = window.open("popup.html"); function doStuffOnUnload() { alert("Unloaded!"); } if (typeof win.attachEvent != "undefined") { win.attachEvent("onunload", doStuffOnUnload); } else if (typeof win.addEventListener != "undefined") { win.addEventListener("unload", doStuffOnUnload, false); } 

If you want the pop-up window to pass information to the main window, I would suggest placing the property in the pop-up window object (for example, window.someValue = 5; ). In doStuffOnUnload() you can get this property: alert(win.someValue);

+6
source

you can attach the onunload event to the child window from the parent

+2
source

All Articles