【问题标题】:window.parent.location.href IE9 Bug?window.parent.location.href IE9 错误?
【发布时间】:2011-07-13 19:21:47
【问题描述】:

场景:

  1. domain1.com 托管 domain2.com 的 iFrame。

  2. domain2.com 正在使用 javascript 来触发需要的动态链接 路由到父窗口。

  3. 链接都是相对路径。

  4. 单击 /linkA.html(在 domain2.com 上的 iframe 中)将父级路由到 domain1.com/linkA.html.

    var _generic = "/linkA.html";

        $("#canvas").each( function () {
            $(this).children("a").attr("href",_generic);
            $(this).click( function(e) {
                e.preventDefault();
                window.parent.location.href = _generic;
            } );
        } );
    

将链接更改为绝对链接 (domain2.com/linkA.html) 解决了功能问题。

以前有人遇到过这种情况吗?

【问题讨论】:

    标签: javascript internet-explorer internet-explorer-9 window.location window.parent


    【解决方案1】:

    为了让浏览器在设置相对路径时解析整个url,首先需要读取当前的href,这被SOP阻止了。

    但我很确定,如果您使用 parent.location 而不是 parent.location.href,那么这将正常工作。

    【讨论】:

      【解决方案2】:

      您应该尝试使用window.opener.location 而不是window.parent.location

      【讨论】:

        【解决方案3】:

        我看到了 IE9 与 IE8、FireFox 和 Webkit 的区别。考虑以下代码:

        父源:

        <!DOCTYPE html>
        <html>
        <head><title>iframe test page</title></head>
        <body>
            <h1>Parent window</h1>
            <iframe src="//domain2/iframe.html"></iframe>
        </body>
        </html>
        

        iframe 源(在单独的域上):

        <!DOCTYPE html>
        <html>
        <head><title>iframe content</title></head>
        <body>
            <h1>iframe content</h1>
            <script>function redirect() { window.parent.location = "/new-href"; } </script>
            <a href="javascript:redirect()">Redirect the parent window please</a>
        </body>
        </html>
        

        如果主页在 IE8 中打开(或 IE9 设置为在 IE8 模式或 Chrome 23 或 FF16 下运行)单击链接会导致导航到 //(iframe domain)/new-href 而在 IE9 中会转到 / /(原始父域)/new-href

        我仍在研究如何解决此问题,如果/当我有更多信息时,我会更新此问题。

        更新:为 iframe 链接设置 target="_top" 属性可以解决此问题。

        更新的 iframe 源(在单独的域上):

        <!DOCTYPE html>
        <html>
        <head><title>iframe content</title></head>
        <body>
            <h1>iframe content</h1>
            <a href="/new-href" target="_top">Redirect the parent window please</a>
        </body>
        </html>
        

        单击此更新的 iframe 代码中的链接会将父(整个)窗口重定向到 IE8 和 IE9 中的 //(iframe domain)/new-href(据我所知,所有浏览器也是如此)。这就是你想做的,对吧?

        例如如果您想让 iframe 中的所有标签导致顶部/主/父窗口导航到某处,而不是 iframe 导航到某处,请使用以下内容:

        $("iframe a").attr("target", "_top");
        

        另见target attribute documentation

        【讨论】:

        • 我在 IE9 中尝试了“window.opener.location”并收到控制台错误(无法设置属性“位置”的值:对象为空或未定义)
        【解决方案4】:

        URL 中的正斜杠隐式文件夹在 Moz 中有效。浏览器,但使用资源管理器,来自同一文件夹的链接不会放置正斜杠。 它只是 X 浏览器的语法。 您可能会做得更好。

        【讨论】:

          猜你喜欢
          • 2018-12-01
          • 1970-01-01
          • 2015-10-27
          • 1970-01-01
          • 1970-01-01
          • 2012-07-10
          • 1970-01-01
          • 1970-01-01
          • 2012-04-21
          相关资源
          最近更新 更多