window.parent.location.href IE9错误?

问题描述 投票:0回答:4

场景:

  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
4个回答
1
投票

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

但是我很确定,如果您使用parent.location而不是parent.location.href,那么就可以了。


1
投票

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


0
投票

我已经看到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域)/ 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域)/ new-href(据我所知,也是所有浏览器)。那就是你想做的,对吗?

例如如果要在iframe中制作所有标签,请使顶部/主/父窗口导航到某处,而不是iframe导航到某处,请使用以下方法:

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

另请参阅target attribute documentation

URL中的斜杠隐式文件夹在Moz中有效。浏览器,但使用资源管理器时,来自同一文件夹的链接不会加正斜杠。这只是X浏览器语法。您可能将./放在更好的位置(用Explorer点正斜杠,但正确的方法是在相同文件夹URL的文件名前没有斜杠)。


0
投票

URL中的斜杠隐式文件夹在Moz中有效。浏览器,但使用资源管理器时,来自同一文件夹的链接不会加正斜杠。这只是X浏览器语法。您可能将./放在更好的位置(用Explorer点正斜杠,但正确的方法是在相同文件夹URL的文件名前没有斜杠)。

© www.soinside.com 2019 - 2024. All rights reserved.