Consider a page that contains an iframe. The iframe source is on another domain.
I’d be interested in 2 things:
-
Can I create an event listener inside the iframe that listens to events that take place in the parent window (and runs a function in the iframe)?
-
Can I create an event listener inside the parent window that listens to events that take place in the iframe (and runs a function in the parent window)?
That’s a no on both accounts, cross-domain scripting security measures disallow any cross domain communication.
You might want to take a look at this indepth article about Cross-Domain Communication with IFrames. It talks about what you can and can’t do, and provides some alternatives like window.postMessage