javascript window.parent.location.href IE9 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6684397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.parent.location.href IE9 Bug?
提问by tgormtx
Scenario:
设想:
domain1.com hosts an iFrame of domain2.com.
domain2.com is using javascript to trigger dynamic links that need to route to the parent window.
The links are all relative paths.
Clicking on /linkA.html (in iframe on domain2.com) routes the parent to 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; } ); } );
domain1.com 托管 domain2.com 的 iFrame。
domain2.com 正在使用 javascript 来触发需要路由到父窗口的动态链接。
链接都是相对路径。
单击 /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; } ); } );
Changing the links to absolute (domain2.com/linkA.html) solves the functional problem.
将链接更改为绝对链接 (domain2.com/linkA.html) 解决了功能问题。
Has anyone run into this before?
有没有人遇到过这个?
回答by Sean Kinsey
In order for the browser to resolve the entire url when setting relative paths, it first needs to read the current href, and this is blocked by the SOP.
为了让浏览器在设置相对路径时解析整个url,首先需要读取当前的href,这被SOP屏蔽了。
But I am pretty sure that if you use parent.location
instead of parent.location.href
, then this will work just fine.
但我很确定,如果你使用parent.location
而不是parent.location.href
,那么这将工作得很好。
回答by Glenn Ferrie
You should try using window.opener.location
instead of window.parent.location
.
您应该尝试使用window.opener.location
而不是window.parent.location
.
回答by iX3
I have seen a difference in IE9 vs. IE8, FireFox, and Webkit. Consider the following code:
我已经看到 IE9 与 IE8、FireFox 和 Webkit 的区别。考虑以下代码:
Parent source:
父源:
<!DOCTYPE html>
<html>
<head><title>iframe test page</title></head>
<body>
<h1>Parent window</h1>
<iframe src="//domain2/iframe.html"></iframe>
</body>
</html>
iframe source (on separate domain):
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>
If the main page is opened in IE8 (or IE9 set to run in IE8 mode or Chrome 23 or FF16) clicking the link results in navigating to //(iframe domain)/new-href whereas in IE9 it goes to //(original parent domain)/new-href
如果主页在 IE8(或 IE9 设置为在 IE8 模式或 Chrome 23 或 FF16 下运行)中打开,单击链接会导航到 //(iframe domain)/new-href 而在 IE9 中它转到 //(original父域)/new-href
I am still investigating how to work around this issue and will update this if/when I have more information.
我仍在调查如何解决这个问题,如果/当我有更多信息时,我会更新这个。
UPDATE: Setting the target="_top" attribute for iframe links works around this problem.
更新:为 iframe 链接设置 target="_top" 属性可以解决这个问题。
Updated iframe source (on separate domain):
更新的 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>
Clicking the link in this updated iframe code will redirect the parent (entire) window to //(iframe domain)/new-href in both IE8 & IE9 (and, as far as I know, all browsers as well). That is what you want to do, right?
单击此更新的 iframe 代码中的链接将在 IE8 和 IE9(以及据我所知,所有浏览器)中将父(整个)窗口重定向到 //(iframe domain)/new-href。这就是你想要做的,对吧?
e.g. If you want to make all tags in an iframe cause the top/main/parent window to navigate somewhere rather than the iframe navigating somewhere then use something like this:
例如,如果您想让 iframe 中的所有标签导致顶部/主窗口/父窗口导航到某处而不是 iframe 导航某处,则使用以下内容:
$("iframe a").attr("target", "_top");
See also target attribute documentation
另请参阅目标属性文档
回答by Samuel Marchant
The forward slash implicit folder in the URL works in Moz. browsers, but with explorer, links from the same folder don not put the forward slash. It's simply X-browser syntax. You may have done better puting ./ (dot forward slash with explorer but the proper way is no slash preceding the filename for a same folder URL).
URL 中的正斜杠隐式文件夹在 Moz 中有效。浏览器,但使用资源管理器,来自同一文件夹的链接不会放置正斜杠。这只是 X 浏览器的语法。您可能已经做得更好了 ./ (使用资源管理器的点正斜杠,但正确的方法是在同一文件夹 URL 的文件名前没有斜杠)。