Redirect parent window from an iframe action

2018-12-31 19:51发布

What JavaScript do I need to use to redirect a parent window from an iframe?

I want them to click a hyperlink which, using JavaScript or any other method, would redirect the parent window to a new URL.

12条回答
闭嘴吧你
2楼-- · 2018-12-31 20:10

This will solve the misery.

<script>parent.location='http://google.com';</script>
查看更多
无色无味的生活
3楼-- · 2018-12-31 20:12

I found that <a href="..." target="_top">link</a> works too

查看更多
美炸的是我
4楼-- · 2018-12-31 20:13

@MIP is right, but with newer versions of Safari, you will need to add sandbox attribute(HTML5) to give redirect access to the iFrame. There are a few specific values that can be added with a space between them.

Reference(you will need to scroll): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe

Ex:

<iframe sandbox="allow-top-navigation" src="http://google.com/"></iframe>
查看更多
像晚风撩人
5楼-- · 2018-12-31 20:21

target="_parent" worked great for me. easy and hassle free!

查看更多
梦醉为红颜
6楼-- · 2018-12-31 20:22
window.top.location.href = "http://example.com";

window.top refers to the window object of the page at the top of the frames hierarchy.

查看更多
皆成旧梦
7楼-- · 2018-12-31 20:22

If you'd like to redirect to another domain without the user having to do anything you can use a link with the property:

target="_parent"

as said previously, and then use:

document.getElementById('link').click();

to have it automatically redirect.

Example:

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

Note: The javascript click() command must come after you declare the link.

查看更多
登录 后发表回答