Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 postMessage returns undefined
    primarykey
    data
    text
    <p>I am trying to do cross-domain communication using postMessage between a page (on example1.com) and an iframe on the same page that is on example2.com</p> <p>When the iframe is done loading, it sends a postMessage to the parent page using the following command:</p> <p><strong>parent.postMessage('hello parent', 'http://example1.com'); //this command works</strong></p> <p>In the event handler for postMessage in the parent page (on example1.com), I send a response back to the child page (example2.com) using the following function:</p> <pre><code>//this function is defined in example1.com: function receiveMessage(event){ if (event.origin == "http://example2.com"){ console.log('Message Received from iframe'); //this line works event.source.postMessage('hello to you too', event.origin); //this line does not work } } </code></pre> <p>For some reason, the event handler for postMessage in the iframe is not triggered.</p> <pre><code>//this function is defined in example2.com function receiveMessage(event){ if (event.origin == "http://example1.com"){ // I even tried removing this check to no avail console.log('Message Received from parent'); //this line works } } </code></pre> <p>When the parent tries to send a message to the iframe it simply returns undefined.</p> <p>Any help is appreciated!</p> <p>EDIT:</p> <p>I tried changing the following line in example1.com's receiveMessage event:</p> <p><strong>event.source.postMessage('hello to you too', event.origin);</strong></p> <p>to this:</p> <p><strong>event.source.postMessage('hello to you too', 'http://example2.com');</strong></p> <p>But I get this following error:</p> <p><strong>Unable to post message to <a href="http://example2.com" rel="nofollow">http://example2.com</a>. Recipient has origin <a href="http://example1.com" rel="nofollow">http://example1.com</a></strong></p> <p>Why is event.origin evaluating to <a href="http://example1.com" rel="nofollow">http://example1.com</a> when it is receiving a message from example2.com?</p> <p>example1.com is trying to send itself a postMessage. This ought to be the source of the error</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload