Javascript:保留窗口对象引用?

发布于 2024-09-24 19:11:57 字数 270 浏览 0 评论 0原文

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

var win = window.open();

我将 win 引用与所有其他打开的窗口一起存储在一个数组中。
一切正常,直到开启器刷新为止。

所以我想要实现的是在加载“主”窗口时取回对打开的窗口的所有引用。

为了实现这一点,我必须:
1.在卸载时保留所有引用 2. 获取加载时的引用 3.用新的开启器更新参考文献

可以吗?如果是这样怎么办?

My javascript code open some windows trough:

var win = window.open();

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-10-01 19:11:57

这听起来很复杂(读作:有问题)。

您所问的问题从很久以前就一直是一个问题,并且可以追溯到原始的 HTTP 协议。

简短的回答是否定的,这实际上是不可能的。当您刷新父级时,对子级的所有引用都会丢失。事实上,这是相当现实的……父母“焕然一新”……孩子们获得了父母留下的所有资源……抱歉,偏离主题了,是的。 Web 是无状态的,因此一旦页面被刷新、重新加载、关闭、打开或附加任何其他动词,该页面就会被视为文档。


这在 chrome/FireFox 中工作正常,但在 IE 中不起作用。

类似下面的内容可以工作,但为了重新连接子/父之间的关系,必须从子中完成。下面的脚本/标记将给出一个示例。
所以我不会使用它,但它只是作为一个例子。

步骤:

  • 打开子项
  • 转到子项
  • 刷新父项
  • **父项 ->孩子的关系就失去了。
  • **儿童->亲子关系依然存在。
  • 链接父级
  • 转到父级页面
  • 检查关系
  • **您将看到父级现在不为空
  • 保持父级
  • 刷新子级

HTMLPage.htm:

<input type="button" onclick="openChild();" value="Open Child" />
<input type="button" onclick="refreshParent();" value="Refresh Parent" />
<input type="button" onclick="linkParent();" value="Link Parent" />
<input type="button" onclick="checkRelationship();" value="Check Relationship" />
<input type="button" onclick="refreshChild();" value="Refresh Child" />


<script type="text/javascript" language="javascript">

    var child;    
    function openChild() {
        child = window.open("HTMLPage.htm");
    }

    function refreshParent() {
        alert("refreshing parent");
        window.opener.location.href = window.opener.location.href;
    }

    function linkParent() {
        window.opener.child = window;
        checkRelationship();
    }

    function checkRelationship() {
        alert("Parent: " + window.opener + ".   Children:" + child);
    }

    function refreshChild() {
        child.location.href = child.location.href;
    }


</script>

那么我的下一个问题是:

您想通过此实现什么目的?当情况确实需要时,我完全赞成使用模式窗口/弹出窗口,但是您需要它们的数组吗?

父页面“需要”刷新吗?您能否在父页面上粘贴一个 iframe 并刷新它(即,文件上传等,其中主页“不需要”刷新)。

我远非苹果迷,但此链接确实提供了有关使用 IFrame 与脚本编写的良好见解: http://developer.apple.com/internet/webcontent/iframe.html

我希望这有帮助。

This sounds very complicated (read: problematic).

What you're asking has been an issue since way back and goes back to the original HTTP protocol.

The short answer is no, this isn't really possible. When you refresh the parent all references to the children are lost. In all actuality it's quite realistic.... parent is "refreshed".... children get all the resources the parent left behind... sorry, off topic and well yea. The Web is stateless, so once a page is refreshed, reloaded, closed, opened, or had any other verb attached to it, the page is treated as a new document.


This works fine in chrome/FireFox, but does not work in IE.

Something like the following will work, but in order to reattach that relationship between child/parent it must be done FROM the child. The script/markup below will give an example.
So I wouldn't use it, but it's here just as an example.

Steps:

  • Open Child
  • Go to Child
  • Refresh Parent
  • **Parent -> Child relationship is lost.
  • **Child -> Parent relationship still exists.
  • Link Parent
  • Go to Parent page
  • Check Relationship
  • **You will see the parent is now not null
  • Stay ON parent
  • Refresh Child

HTMLPage.htm:

<input type="button" onclick="openChild();" value="Open Child" />
<input type="button" onclick="refreshParent();" value="Refresh Parent" />
<input type="button" onclick="linkParent();" value="Link Parent" />
<input type="button" onclick="checkRelationship();" value="Check Relationship" />
<input type="button" onclick="refreshChild();" value="Refresh Child" />


<script type="text/javascript" language="javascript">

    var child;    
    function openChild() {
        child = window.open("HTMLPage.htm");
    }

    function refreshParent() {
        alert("refreshing parent");
        window.opener.location.href = window.opener.location.href;
    }

    function linkParent() {
        window.opener.child = window;
        checkRelationship();
    }

    function checkRelationship() {
        alert("Parent: " + window.opener + ".   Children:" + child);
    }

    function refreshChild() {
        child.location.href = child.location.href;
    }


</script>

So then my next question is:

What are you trying to achieve with this? I'm all for using a modal window / pop up window when the situation really calls for one, but do you need an array of them?

Does the parent page "need" to refresh? Could you stick an iframe on the parent page and refresh that (IE. file uploads etc. where the main page doesn't "need" to refresh).

I'm far from an apple fan boy, but this link does give a good insight on using IFrames with scripting: http://developer.apple.com/internet/webcontent/iframe.html

I hope this helps.

胡渣熟男 2024-10-01 19:11:57

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

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

在主窗口的卸载事件上,我在子窗口上调用一个设置超时的 javaScript 函数。当超时结束时,我测试开启器参考并尝试重新注册,因为参考仍然正确。

这样我就可以拿回所有的子参考资料!! ;)

当然,如果主窗口中加载的文档不是我的文档,但例如 www.google.com 注册回子引用会失败。

该方案已在Firefox、IE、Chrome下测试。

*在 Chrome 中,当您输入新地址时,它会启动一个新窗口,因此 opener 也将为 null。

How I solved the issue.

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*

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.

This way I have all the child references back!! ;)

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.

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

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

情魔剑神 2024-10-01 19:11:57

要么避免刷新主窗口(使用 AJAX 或 iframe),要么将“主窗口”放入框架集中,这样实际上只有“主框架”被重新加载 - 然后您可以将变量粘贴到父框架中

either avoid refrehsing the main window (use AJAX or iframes), or put the "main window" into a frameset so that in fact only the "main frame" is reloaded - then you can stick the variables into the parent frame

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文