Access parent window from iframe (cross-domain)

101,254

Solution 1

If I were you I would check out window.postMessage. It may do what you want:

For reference see the following:

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:

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.

Share:
101,254
Vitaliy Lebedev
Author by

Vitaliy Lebedev

Updated on July 09, 2022

Comments

  • Vitaliy Lebedev
    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
    Vitaliy Lebedev about 12 years
    I 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
    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
    Vitaliy Lebedev about 12 years
    The 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
    user854301 about 11 years
    You 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
    Don Ho over 10 years
    As others have pointed out, this actually is possible, via postMessage for example. So @jeremysawesome answer should be the accepted one.
  • Molomby
    Molomby over 9 years
    This is the correct answer. Cross-domain js calls between windows (or iframes) are now possible in HTML5 using Window.postMessage.
  • frandroid
    frandroid almost 8 years
    Except on IE < EDGE, when using in windows/frames across domains.
  • Sudi
    Sudi almost 7 years
    Question was cross domain access. You can't access local storage/session storage cross domain?
  • Gerd Dirk Zweäns
    Gerd Dirk Zweäns almost 7 years
    you can use it cross domain if you know the names of the variables
  • 255.tar.xz
    255.tar.xz about 5 years
    my answer allows you to do this without being in control of both pages!
  • 255.tar.xz
    255.tar.xz about 5 years
    yes, 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
    lmblanes almost 5 years
    this is only possible if you have control of the parent and child elements, what about only having access to the child element?