Access parent window from iframe (cross-domain)
Solution 1
If I were you I would check out window.postMessage. It may do what you want:
For reference see the following:
- MDN - Window.postMessage
- https://stackoverflow.com/a/3076648/296889 - see the Window.postMessage section
Solution 2
If I understand correctly, all modern browsers do now allow to do this. So I'm here to find the best solution.
This is your solution. What you're asking is not possible.
See related questions:
- How to access parent Iframe from JavaScript
- <iframe> javascript access parent DOM across domains?
- Cross-domain access in iframe from child to parent
EDIT
As mentioned in the comments below, @JeremysAwesome's answer offers a method that would allow cross-domain requests under certain circumstances. See the SO question below for more information.
Ways to circumvent the same-origin policy
Solution 3
but you can change the src
attribute of the iframe
(adding a #hashtag
for example) and listen to the onhashchange
event in the child window. Given that you're in position to change both pages.
Vitaliy Lebedev
Updated on July 09, 2022Comments
-
Vitaliy Lebedev almost 2 years
I've encountered the task to access parent window from iFrame, if the window in iFrame was loaded from another domain. If I understand correctly, all modern browsers do now allow to do this. So I'm here to find the best solution.
I'm going to accomplish this next way:
I have an overlay with an iFrame within it. This will work instead of pop-ups to prevent pop-up blockers to block my content. The task is to reload the main page when the document in the iFrame finishes some work. In a document, which will be loaded to an iFrame, I will add
<div id="is_closed" class="false"></div>
In a parent window I'll add function, which will be called every second and check if this div still has class name "false". When this will be changed to "true", I'll call some callbacks.
If you have any better solution, please share it with me. Will appreciate any help.
Edit: This is impossible because it's not only impossible to manipulate parent window from child window, but vice versa too. My idea was to manipulate child window from parent window. I was wrong.
-
Vitaliy Lebedev about 12 yearsI understand that this is impossible to manipulate parent window from child window. What I'm looking for is how to imitate this manipulation. In other words, some flag from child window on which parent window will run some piece of code, will do.
-
James Hill about 12 years@Malgin, again, this is not possible. You're asking for script level interaction between the pages. This is not possible. You cannot imitate something that cannot be done. There are lots of questions on SO that deal with this. See edit.
-
Vitaliy Lebedev about 12 yearsThe only thing that was not clear to me from you answers is that it is impossible to manipulate child window from parent window either. That was my thought that made me be more persistant. Thanks again.
-
user854301 about 11 yearsYou can, however, change the window location. If you know the URLs in question and have control of both domains you could probably use query strings to send messages between the two. Sounds like you should pursue a better solution though.
-
Don Ho over 10 yearsAs others have pointed out, this actually is possible, via postMessage for example. So @jeremysawesome answer should be the accepted one.
-
Molomby over 9 yearsThis is the correct answer. Cross-domain js calls between windows (or iframes) are now possible in HTML5 using Window.postMessage.
-
frandroid almost 8 yearsExcept on IE < EDGE, when using in windows/frames across domains.
-
Sudi almost 7 yearsQuestion was cross domain access. You can't access local storage/session storage cross domain?
-
Gerd Dirk Zweäns almost 7 yearsyou can use it cross domain if you know the names of the variables
-
255.tar.xz about 5 yearsmy answer allows you to do this without being in control of both pages!
-
255.tar.xz about 5 yearsyes,
window.postmessage
is the way to go... HOWEVER, you don't always have access to the child iframes code! so if you use your answer combined with mine you have a fully working solution! -
lmblanes almost 5 yearsthis is only possible if you have control of the parent and child elements, what about only having access to the child element?