Redirect parent window from an iframe action

JavascriptIframeDhtml

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Will redirect the top most parent Iframe.

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

Will redirect the parent iframe.

Solution 2 - Javascript

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

Solution 3 - Javascript

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

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

Solution 4 - Javascript

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

Solution 5 - Javascript

or an alternative is the following (using document object)

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

Solution 6 - Javascript

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

Solution 7 - Javascript

This will solve the misery.

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

Solution 8 - Javascript

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.

Solution 9 - Javascript

It is possible to redirect from an iframe, but not to get information from the parent.

Solution 10 - Javascript

For current page - window.location.href = "Your url here";

For Parent page - window.top.location.href = "Your url here";

From HTML

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

Solution 11 - Javascript

Try using

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

Solution 12 - Javascript

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

This will redirect the main window to the index page. Thanks

Solution 13 - Javascript

We have to use window.top.location.href to redirect parent window from an iframe action.

Demo url :

Solution 14 - Javascript

Redirect iframe in parent window by iframe in the same parent:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAliView Question on Stackoverflow
Solution 1 - JavascriptMIPView Answer on Stackoverflow
Solution 2 - JavascriptAliView Answer on Stackoverflow
Solution 3 - JavascriptLuca MatteisView Answer on Stackoverflow
Solution 4 - JavascriptrDroidView Answer on Stackoverflow
Solution 5 - JavascriptReal Red.View Answer on Stackoverflow
Solution 6 - JavascriptRaymond BView Answer on Stackoverflow
Solution 7 - JavascriptharisView Answer on Stackoverflow
Solution 8 - JavascriptTom BurrisView Answer on Stackoverflow
Solution 9 - JavascriptJoris KokView Answer on Stackoverflow
Solution 10 - JavascriptBoris AdamyanView Answer on Stackoverflow
Solution 11 - JavascriptChirag GohelView Answer on Stackoverflow
Solution 12 - JavascriptJohn SheedyView Answer on Stackoverflow
Solution 13 - JavascriptFaizalView Answer on Stackoverflow
Solution 14 - Javascriptmohamed moren3View Answer on Stackoverflow