0

I'm getting very confused reading all the examples about how to use window.postMessage() with iframes because they all involve one page being notified of activity, like clicks, happening in the other. Or else they only output some text to the console. I just want to get the original iframe embedding working, nothing else.

I have a page at https://subdomain.example.com/page1/ that contains an iframe like this:

<iframe src="https://example.com/page2/" name="myIframe"></iframe>

Obviously this doesn't work because the origins are different. How do I make it work using postMessage()? Once the iframe has loaded the pages don't need to communicate or know anything about each other.

Phil Gyford
  • 13,432
  • 14
  • 81
  • 143
  • 2
    If the pages don't need to communicate with each other, you don't need postMessage(). The iframe code you've posted above should work just fine. – Daniel Beck Sep 15 '22 at 12:08
  • That would explain my confusion then :) I assumed the blank iframe and a message in Safari about "SecurityError: Blocked a frame with origin "https://subdomain.example.com" from accessing a cross-origin frame. Protocols, domains, and ports must match." was because of this, but it must be some other problem. Thank you for responding. – Phil Gyford Sep 15 '22 at 14:12
  • CORS issues are a whole other ball game; AFAIK they shouldn't come into play for a simple `iframe src=""` unless either the parent or child window is trying to access the other via javascript. Is the domain you're trying to put inside the iframe under your control? If so check its allowed-origins and maybe make sure the X-Frame-Options header isn't being set (though that's probably not it, it would give you a different error message) – Daniel Beck Sep 15 '22 at 15:15

0 Answers0