Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for parent window to notice if child window has been closed?

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

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

Let's say, in parent page, I have js function hello()

In order for child to call parent's hello() when the child window is closed and also pass an argument, I can do,

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

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

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

One thing I can think of is:

Somewhat parent knows when the child is closed (event listenr??? not sure in js) But in such case how to receive the argument? (i.e. some data back from the child)

like image 816
Meow Avatar asked Jan 17 '11 01:01

Meow


2 Answers

The obvious solution (adding an onunload event handler property to the pop-up window object) won't work in IE. However, using attachEvent does work in IE, so the following will do the job:

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'd suggest placing a property in the pop-up's window object (e.g. window.someValue = 5;). In doStuffOnUnload(), you can then pick up on that property: alert(win.someValue);

like image 50
Tim Down Avatar answered Oct 20 '22 09:10

Tim Down


you could attach an onunload event to the child window from the parent

like image 37
hunter Avatar answered Oct 20 '22 07:10

hunter