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.
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.
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.
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 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 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 but is neither same-origin with its target
–
Clockmaker I found that <a href="..." target="_top">link</a>
works too
window.top.location.href = "http://example.com";
window.top
refers to the window object of the page at the top of the frames hierarchy.
target="_parent"
worked great for me. easy and hassle free!
or an alternative is the following (using document object)
parent.document.location.href = "http://example.com";
parent.location.href = ...
. developer.mozilla.org/en/document.location –
Supat @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>
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 –
Gee This will solve the misery.
<script>parent.location='http://google.com';</script>
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.
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>
It is possible to redirect from an iframe, but not to get information from the parent.
Try using
window.parent.window.location.href = 'http://google.com'
parent.location = 'url'
–
York window.top.location.href = 'index.html';
This will redirect the main window to the index page. Thanks
We have to use window.top.location.href to redirect parent window from an iframe action.
Demo url :
Redirect iframe in parent window by iframe in the same parent:
window.parent.document.getElementById("content").src = "content.aspx?id=12";
© 2022 - 2024 — McMap. All rights reserved.
IFrame
, too. Since the accepted answer addresses the top window, I suggest you change your question a bit. – Blamed