从iframe操作重定向父窗口

时间:2009-02-24 06:24:34

标签: javascript iframe dhtml

我需要使用哪些JavaScript来从iframe重定向父窗口?

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

14 个答案:

答案 0 :(得分:467)

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

如前所述,将重定向父iframe。

答案 1 :(得分:104)

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

答案 2 :(得分:53)

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

window.top指的是帧层次结构顶部页面的窗口对象。

答案 3 :(得分:15)

或替代方案如下(使用文档对象)

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

答案 4 :(得分:9)

target="_parent"对我很有用。轻松无忧!

答案 5 :(得分:7)

@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>

答案 6 :(得分:2)

这将解决痛苦。

<script>parent.location='http://google.com';</script>

答案 7 :(得分:1)

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

答案 8 :(得分:1)

window.top.location.href = 'index.html';

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

答案 9 :(得分:1)

如果您想在没有用户做任何事情的情况下重定向到另一个域,您可以使用该属性的链接:

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()命令。

答案 10 :(得分:0)

尝试使用

window.parent.window.location.href = 'http://google.com'

答案 11 :(得分:0)

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

Demo url

答案 12 :(得分:0)

对于当前页面-window.location.href =“您的网址在这里”;

对于“父”页面-window.top.location.href =“您的网址在这里”;

来自HTML

<a href="http://someurl" target="_top">link</a>

答案 13 :(得分:-9)

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

window.parent.document.getElementById("content").src = "content.aspx?id=12";
相关问题