且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript:持久化窗口对象引用?

更新时间:2023-01-17 16:09:24

我是如何解决这个问题的。

How I solved the issue.

子窗口opener属性保持对打开它的WINDOW的引用,因此当刷新主窗口时窗口不会改变,因此开启者引用仍然是相同的。
当opener为null时唯一的情况是窗口关闭时*

The child window "opener" property keeps a reference to the WINDOW that opened it, so when the master window is refreshed the window doesn't change, so the opener reference is still the same. The only case when the opener is null is when the window is closed*

在主窗口的unload事件中,我调用了一个javaScript函数设置超时的子窗口。当超时结束时,我测试开启者引用并尝试注册,因为引用仍然正确。

On the unload event of the master window, I call a javaScript function on the child window that sets a timeout. When the timeout ends I test the opener reference and try to register back because the reference is still correct.

这样我就有了所有的子引用! ;)

当然,如果主窗口中加载的文档不是我的文档,但是,例如,www.google.com注册了儿童参考失败。

Of course, if the document loaded in the master window is not my document, but, for example, www.google.com registering back the child reference fails.

此解决方案已在Firefox,IE和Chrome下测试。

This solution has been teste under Firefox, IE and Chrome.

*在Chrome时你键入一个新的地址,它开始一个新的窗口,因此开启者也将为空。

*in Chrome when you type a new address it start a new window, so opener will be null as well.