Javascript:持久化窗口对象引用? [英] Javascript: persist window object reference?

查看:104
本文介绍了Javascript:持久化窗口对象引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript代码打开了一些窗口:

My javascript code open some windows trough:

var win = window.open();

我将win参与存储在一个包含所有其他打开窗口的数组中。

一切正常,直到开启者刷新。

I store the win refernce in an array with all the other opened windows.
Everything works fine, until the opener is refreshed.

所以我想要获得的是当主人时取回所有对已打开窗口的引用窗口已加载。

So what i'd like to achive is to get back all the references to the opened windows when the "master" window is loaded.

为了实现这一点,我必须:

1.在卸载时保留所有引用
2.在load $上获取引用b $ b 3.使用新的开瓶器更新参考资料

To realize this i have to:
1. persist all the references on unload 2. get back the references on load 3. update the references with the new opener

有可能吗?如果是这样的话?

Is it possible? if so how?

推荐答案

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

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.

这篇关于Javascript:持久化窗口对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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