Why are messages not being sent to the iframe?

  • 0
    Good day.

    The page has an iframe with src http://child.com/widget/ . A message is sent to it from the parent (the domain is different, let it be http://parent.com ) using
    iframe.contentWindow.postMessage("confidential information string", "http://child.com/widget/")
    On the iframe side, the message is received using window.addEventListener ('message', foo) . But when sent using the above method, messages do not come.

    I've tried changing the targetOrigin in postMessage to http://child.com/ or http://child.com but that doesn't help. Messages are received only if you specify "*" , which is unsafe, because the message contains confidential data.

    From the documentation I read, everything should work correctly, however, it doesn't.

    What could be the reason for this strange behavior?
    JavaScript Anonymous, Apr 10, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!