【问题标题】:How can I get the reference of the opened child window within the parent window?如何在父窗口中获取打开的子窗口的引用?
【发布时间】:2019-03-23 19:46:55
【问题描述】:

我需要从父窗口更新子窗口的数据。

为此,我需要获取打开的子窗口的引用并将信息传递给名为UpdateInfo() 的子函数。

这是在父网页中打开子窗口的行:

window.open("HTMLPage2.htm" , "child" , "width=200, height=100")

知道如何在父窗口中获取打开的子窗口的引用吗?

附:我不知道我的方法是否正确;有更好的方法吗?

【问题讨论】:

  • var myChild = window.open("HTMLPage2.htm" , "child" , "width=200, height=100") => 然后您可以使用 myChild 引用窗口

标签: javascript html web client


【解决方案1】:

window.open 返回子窗口的引用。你可以使用这样的东西:

var child = window.open("HTMLPage2.htm" , "child" , "width=200, height=100");
child.updateInfo(...);

这只有在孩子的 URL 符合 Same-Origin Policy 时才有可能。

编辑
如果子不是来自同一来源,您仍然可以通过window.postMessage() API 与它通信。这使您可以向子窗口发送消息事件,子窗口可以监听该事件并调用自定义功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多