【问题标题】:Issue accessing parent URL from child window从子窗口访问父 URL 的问题
【发布时间】:2013-09-30 23:06:36
【问题描述】:

假设父窗口有 abc.xyz.com url,我正在使用以下方法打开一个子窗口:window.top.open

我正在使用 window.opener 在子窗口中获取父窗口对象,并使用 window.opener.location.href 轮询父窗口 url。

现在,如果用户单击父窗口中的链接,该链接导航到 def.xyz.com,则 window.opener.location.href 给出“权限被拒绝”并且 window.opener.closed 返回 true。 (在子脚本中)

如果父窗口不在 xyz.com 内,我需要更改子窗口

我如何知道父级导航到 xyz.com 或其他域?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    类似这样的:

    query = window.parent.location.search.substring(1)
    

    引用自 HEREThis 也可以帮忙。

    【讨论】:

      【解决方案2】:

      您可以在 parent 窗口中添加一些 javascript,以便在其 URL 更改时修改子窗口。使用window.onunload 事件就可以了:

      window.onunload=function(){
          childWindow.location.href = 'parentURLChanged.html';
      };
      

      除了完全更改位置,您还可以只在 URL 中添加 hash,这样它就会从 child.html 更改为 child.html#parentHasChanged
      如果您想在这两个窗口之间进行更完整的通信,我建议您使用jQuery postMessage Plugin。它允许您在两个 Windows 或框架之间发送和接收消息,并且还可以跨域

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多