Redirect parent window from an iframe action

Redirect parent window from an iframe action

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.

Solutions/Answers:

Solution 1:

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

As stated previously, will redirect the parent iframe.

Solution 2:

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

Solution 3:

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:

or an alternative is the following (using document object)

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

Solution 5:

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

Solution 6:

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