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

谁都会走 提交于 2019-12-05 05:29:51

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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!