我需要使用什么 JavaScript 从 iframe 重定向父窗口?
我希望他们单击一个超链接,使用 JavaScript 或任何其他方法将父窗口重定向到一个新的 URL。
我需要使用什么 JavaScript 从 iframe 重定向父窗口?
我希望他们单击一个超链接,使用 JavaScript 或任何其他方法将父窗口重定向到一个新的 URL。
window.top.location.href = "http://www.example.com";
将重定向最顶层的父 iframe。
window.parent.location.href = "http://www.example.com";
将重定向父 iframe。
我发现这<a href="..." target="_top">link</a>
也有效
window.top.location.href = "http://example.com";
window.top
指的是位于框架层次结构顶部的页面的窗口对象。
target="_parent"
对我来说很好。轻松无忧!
或以下替代方法(使用文档对象)
parent.document.location.href = "http://example.com";
@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>
这将解决苦难。
<script>parent.location='http://google.com';</script>
如果您想在用户无需执行任何操作的情况下重定向到另一个域,您可以使用该属性的链接:
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 click() 命令必须在您声明链接之后出现。
可以从 iframe 重定向,但不能从父级获取信息。
对于当前页面 - window.location.href = "Your url here";
对于父页面 - window.top.location.href = "Your url here";
从 HTML
<a href="http://someurl" target="_top">link</a>
尝试使用
window.parent.window.location.href = 'http://google.com'
window.top.location.href = 'index.html';
这会将主窗口重定向到索引页面。谢谢
我们必须使用 window.top.location.href 从 iframe 操作重定向父窗口。
演示网址:
通过同一父级中的 iframe 重定向父窗口中的 iframe:
window.parent.document.getElementById("content").src = "content.aspx?id=12";