从iframe动作重定向父窗口
window.top.location.href = "http://www.example.com";
如前所述,将重定向父iframe中。
window.top.location.href = "http://example.com";
window.top
引用框架层次结构顶部页面的窗口对象。
window.top.location.href属性无法从iframe更新,它会抛出访问被拒绝的错误。它只能在你在本地主机上测试时执行 – 2010-06-05 15:40:16
@Ummar:我会补充说,当父节点的iframe具有相同的域,相同的端口(相同的源策略)时,它会起作用,当它们不同时引发访问被拒绝异常,以防止安全漏洞 – Vimvq1987 2012-04-04 03:11:49
@Ummar不是真实的,http://jsfiddle.net/ppkzS/用于证明。你可以改变window.top.location.href。你只是看不懂。在铬中工作。 – Parris 2013-08-06 06:18:26
或替代是下述(使用文档对象)
parent.document.location.href = "http://example.com";
该解决方案是Firefox特定的。一个应该只使用`parent.location.href = ...`。 https://developer.mozilla.org/en/document.location – kay 2012-07-12 23:42:16
我发现<a href="..." target="_top">link</a>
工程太
可以从iframe重定向,但不能从父级获取信息。
这将解决苦难。
<script>parent.location='http://google.com';</script>
尝试在同一父使用父窗口
window.parent.window.location.href = 'http://google.com'
重定向的iframe通过iframe的:
window.parent.document.getElementById("content").src = "content.aspx?id=12";
window.top.location.href = 'index.html';
这将在主窗口重定向到索引页面。 谢谢
@MIP是正确的,但有了更新版本的Safari,您需要添加沙盒属性(HTML5)以重定向访问iFrame。有几个特定的值可以在它们之间添加一个空格。
参考(您需要滚动): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
例:
<iframe sandbox="allow-top-navigation" src="http://google.com/"></iframe>
target="_parent"
工作对我来说太棒了。容易和无忧无虑!
如果您想重定向到另一个域,而无需用户做任何事情,你可以使用带有属性的链接:正如前文所说
target="_parent"
,然后用:
document.getElementById('link').click();
让它自动重定向。
例子:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<a id="link" target="_parent" href="outsideDomain.html"></a>
<script type="text/javascript">
document.getElementById('link').click();
</script>
</body>
</html>
注:JavaScript的点击()命令必须来声明的链接后。
**父**窗口本身也可以是`IFrame`。由于接受的答案解决了**顶部**窗口,所以我建议你稍微改变一下你的问题。 – 2012-10-10 19:33:13