401

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.

1
  • 5
    The parent window, in itself, could be an IFrame, too. Since the accepted answer addresses the top window, I suggest you change your question a bit. Commented Oct 10, 2012 at 19:33

14 Answers 14

645
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.

Sign up to request clarification or add additional context in comments.

11 Comments

I don't think same origin policy applies here. It makes sense that you should be able to break your site out of someone else's iframe.
In Chrome, @Parris jsfiddle throws this error: Unsafe JavaScript attempt to initiate navigation for frame with URL 'jsfiddle.net/ppkzS' from frame with URL 'parrisstudios.com/tests/iframe_redirect.html'. The frame attempting navigation of the top-level window is sandboxed, but the 'allow-top-navigation' flag is not set.
@BjarteAuneOlsen interesting, might be a recent change in chrome or webkit. It was definitely working previously. It has been a couple years :). Also the answer used to state that same origin policy applied, which previously made the answer incorrect, but now makes it correct and my comment wrong -_-.
@BjarteAuneOlsen - I believe this is because the Iframes on JSFiddle are deliberately sandboxed so they have explicitly denied certain features (such as redirect, etc) - there's a danger that you'd use JS to navigate away from JS Fiddle and lose your work...
The errors people are encountering here are because of a new HTML5 attribute for iframes called, "sandbox" - developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe The sandbox attribute prevents javascript taking certain actions within an iframe based on a given whitelist of allowed actions. The allow-top-navigation property is one of the actions you can enable in the whitelist, which allows this code to work. If the sandbox attribute is abscent all actions are allowed. If it is present and an empty string, all actions are denied.
|
159

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

3 Comments

While the selected answer is correct, I think this is a better answer for the question's intent. Also, it will work for the people who have JS disabled.
I agree this is the best solution but the OP specifically asked for javascript. On the other hand, OP specified it should be trigger via an anchor - I can't see a scenario where one would be forced to use javascript from an anchor tag to achieve the desired result, therefor the question is misleading.
I need to redirect parent iframe from child iframe. Tried _parent but it's not working in firefox.
64
window.top.location.href = "http://example.com";

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

6 Comments

window.top.location.href property can't be updated from Iframe, it throws access denied execption. It can only be executed when you are testing on localhost
@Ummar: I would add that this works when the parent an the iframe has same domain, same port (same origin policies), it throws access denied exception when they're different, to prevent security breaches
@Ummar that isn't true, jsfiddle.net/ppkzS for proof. You can change window.top.location.href. You just can't read it. Works in chrome.
Great jsfiddle @Parris, that settles it for me :)
I suspect some of the doubters may have been trying something like this within in the iframe: window.top.location.href = window.top.location.href;
|
23

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

2 Comments

These seems to work great. Are there any downsides to using this method rather than Javascript?
not that I know of. DO share if you find out something.
17

or an alternative is the following (using document object)

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

2 Comments

That solution is Firefox specific. One should just use parent.location.href = .... developer.mozilla.org/en/document.location
Solution not worked for me. Chrome in windows 10 says 'Redirect blocked'. If user does not allow, this solution will not work
11

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

1 Comment

Note that the sandbox attribute enables an extra set of restrictions for the content in the <iframe>. The value of the sandbox property removes particular restrictions. So watch out with using this feature. w3schools.com/tags/att_iframe_sandbox.asp
4

This will solve the misery.

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

Comments

4

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.

Comments

2

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>

Comments

1

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

Comments

0

Try using

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

2 Comments

Simple version: parent.window.location = '<URL>';
or further simplified: parent.location = 'url'
0
window.top.location.href = 'index.html';

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

1 Comment

Duplicate of this answer from almost 5 years earlier.
-3

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

Demo url :

1 Comment

Who is we? :) Could you write the essential parts from the url into the answer please? URLs/links may change or get deleted.
-11

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.