父窗口是否可能注意到子窗口是否已关闭? [英] Is it possible for parent window to notice if child window has been closed?

查看:105
本文介绍了父窗口是否可能注意到子窗口是否已关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有parent window (opener)child (popup)

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

比方说,在父页面中,我有js函数hello()

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

为了让孩子可以在关闭子窗口时调用父对象的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);

这将关闭窗口并调用父级的hello();

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

但是,如果我不想在子页面中包含代码window.opener.hello()怎么办?我的意思是我只希望代码位于父页面中

我能想到的一件事是:

有些家长知道孩子何时关闭(事件侦听器?在js中不确定) 但是在这种情况下如何接收论点呢? (即从孩子那里返回的一些数据)

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)

推荐答案

显而易见的解决方案(将onunload事件处理程序属性添加到弹出窗口对象中)在IE中不起作用.但是,使用attachEvent确实可以在IE中正常工作,因此可以通过以下方式完成工作:

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

如果您希望弹出窗口将信息传递到主窗口,建议您在弹出窗口的window对象(例如window.someValue = 5;)中放置一个属性.然后,在doStuffOnUnload()中,可以选择该属性:alert(win.someValue);

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

这篇关于父窗口是否可能注意到子窗口是否已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆