Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access parent window from iframe (cross-domain)

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.

like image 373
Vitaliy Lebedev Avatar asked Mar 20 '12 14:03

Vitaliy Lebedev


3 Answers

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
like image 158
jeremysawesome Avatar answered Nov 13 '22 22:11

jeremysawesome


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

like image 31
James Hill Avatar answered Nov 13 '22 23:11

James Hill


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.

like image 7
grilly Avatar answered Nov 13 '22 21:11

grilly