360

我需要使用什么 JavaScript 从 iframe 重定向父窗口?

我希望他们单击​​一个超链接,使用 JavaScript 或任何其他方法将父窗口重定向到一个新的 URL。

4

14 回答 14

593
window.top.location.href = "http://www.example.com"; 

将重定向最顶层的父 iframe。

window.parent.location.href = "http://www.example.com"; 

将重定向父 iframe。

于 2010-07-07T08:41:58.050 回答
138

我发现这<a href="..." target="_top">link</a>也有效

于 2009-02-24T11:31:02.610 回答
57
window.top.location.href = "http://example.com";

window.top指的是位于框架层次结构顶部的页面的窗口对象。

于 2009-02-24T06:26:55.600 回答
17

target="_parent"对我来说很好。轻松无忧!

于 2014-11-25T12:51:13.957 回答
14

或以下替代方法(使用文档对象)

parent.document.location.href = "http://example.com";
于 2009-02-24T10:19:06.417 回答
9

@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>
于 2014-10-23T21:39:40.510 回答
3

这将解决苦难。

<script>parent.location='http://google.com';</script>
于 2012-07-06T10:50:25.340 回答
3

如果您想在用户无需执行任何操作的情况下重定向到另一个域,您可以使用该属性的链接:

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() 命令必须在您声明链接之后出现。

于 2016-03-26T05:23:33.960 回答
1

可以从 iframe 重定向,但不能从父级获取信息。

于 2012-02-08T13:17:32.100 回答
1

对于当前页面 - window.location.href = "Your url here";

对于父页面 - window.top.location.href = "Your url here";

从 HTML

<a href="http://someurl" target="_top">link</a>
于 2020-11-02T14:21:53.457 回答
0

尝试使用

window.parent.window.location.href = 'http://google.com'
于 2012-10-31T06:08:07.117 回答
0
window.top.location.href = 'index.html';

这会将主窗口重定向到索引页面。谢谢

于 2013-11-08T04:58:51.710 回答
-3

我们必须使用 window.top.location.href 从 iframe 操作重定向父窗口。

演示网址

于 2019-05-16T11:39:51.353 回答
-8

通过同一父级中的 iframe 重定向父窗口中的 iframe:

window.parent.document.getElementById("content").src = "content.aspx?id=12";
于 2013-08-27T19:56:08.820 回答