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

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


当前回答

或者另一种选择如下(使用文档对象)

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

其他回答

对于当前页面- window.location.href = "你的url在这里";

对于父页面- window.top.location.href = "你的url在这里";

从HTML

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

我发现<a href="…" target="_top">link</a>也能工作

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

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

或者另一种选择如下(使用文档对象)

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

@MIP是正确的,但是对于更新版本的Safari,你将需要添加沙盒属性(HTML5)来重定向访问iFrame。有一些特定的值可以在它们之间添加一个空格。

参考(你需要滚动): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe

Ex:

<iframe sandbox="allow-top-navigation" src="http://google.com/"></iframe>