Redirect parent window from an iframe action
Asked Answered
F

14

391

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.

Footstool answered 24/2, 2009 at 6:24 Comment(1)
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.Blamed
M
634
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.

Mcgregor answered 7/7, 2010 at 8:41 Comment(11)
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.Depress
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.Beep
@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 -_-.Alcahest
@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...Thromboplastic
I tested with latest Safari (10.0.3 (12602.4.8)), I got this Console error: Unsafe JavaScript attempt to initiate navigation for frame with URL 'http://jsfiddle.net/ppkzS/' from frame with URL 'http://www.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.Nester
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.Liquidator
It's not valid in some conditions from Chrome 68: The frame attempting navigation is targeting its top-level window, but is neither same-origin with its target nor has it received a user gesture.Clockmaker
@Clockmaker The OP clearly says navigation is intended to be though click on a hyperlink. Click is a user gesture.Baylor
@Baylor but is neither same-origin with its targetClockmaker
@Clockmaker It's not an issue as long as user interacts with the frame.Baylor
Both solution not worked for me. Chrome in windows 10 says 'Redirect blocked'. If user does not allow, this solution will not work.Barren
F
158

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

Footstool answered 24/2, 2009 at 11:31 Comment(3)
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.Vasquez
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.Bismuthic
I need to redirect parent iframe from child iframe. Tried _parent but it's not working in firefox.Howell
S
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.

Scully answered 24/2, 2009 at 6:26 Comment(6)
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 localhostFate
@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 breachesManslaughter
@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.Alcahest
Great jsfiddle @Parris, that settles it for me :)Semilunar
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;Seifert
This answer came way before the accepted answer, why doesn't it have the accept and upvotes?Buffum
S
23

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

Sequester answered 25/11, 2014 at 12:51 Comment(3)
These seems to work great. Are there any downsides to using this method rather than Javascript?Virgil
not that I know of. DO share if you find out something.Sequester
this worked perfectlyTohubohu
M
16

or an alternative is the following (using document object)

parent.document.location.href = "http://example.com";
Michelinemichell answered 24/2, 2009 at 10:19 Comment(2)
That solution is Firefox specific. One should just use parent.location.href = .... developer.mozilla.org/en/document.locationSupat
Solution not worked for me. Chrome in windows 10 says 'Redirect blocked'. If user does not allow, this solution will not workBarren
B
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>
Barling answered 23/10, 2014 at 21:39 Comment(1)
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.aspGee
C
4

This will solve the misery.

<script>parent.location='http://google.com';</script>
Choose answered 6/7, 2012 at 10:50 Comment(0)
T
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.

Turkey answered 26/3, 2016 at 5:23 Comment(0)
R
3

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>
Rissole answered 2/11, 2020 at 14:21 Comment(0)
C
1

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

Criseldacrisey answered 8/2, 2012 at 13:17 Comment(0)
A
0

Try using

window.parent.window.location.href = 'http://google.com'
Aggregation answered 31/10, 2012 at 6:8 Comment(2)
Simple version: parent.window.location = '<URL>';Oleo
or further simplified: parent.location = 'url'York
A
0
window.top.location.href = 'index.html';

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

Arable answered 8/11, 2013 at 4:58 Comment(1)
Duplicate of this answer from almost 5 years earlier.Vasquez
S
-3

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

Demo url :

Sixtynine answered 16/5, 2019 at 11:39 Comment(1)
Who is we? :) Could you write the essential parts from the url into the answer please? URLs/links may change or get deleted.Beer
P
-9

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

window.parent.document.getElementById("content").src = "content.aspx?id=12";
Pepper answered 27/8, 2013 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.