Something fails there about the origin of the event with the postMessage javascript event.
Here is my main page:
<html> <body> <h1>Test</h1> <h2>Outside</h2> <iframe src="iframe-include.html" width="100%" height="100" sandbox="allow-scripts"></iframe> <script type="text/javascript"> window.addEventListener('message', function (event) { console.log(event); }, false); </script> </body> </html>
And my iFrame content
<html> <body> <h3>Inside</h3> <script type="text/javascript"> var counter = 1, domain = window.location.protocol + '//' + window.location.host, send = function () { window.setTimeout(function () { console.log('iframe says:', domain); window.parent.postMessage(counter, domain); counter += 1; send(); }, 3000); }; send(); </script> </body> </html>
Looking at the console, the origin property of the event object is always null, even if the domain variable in the iFrame is correct.
My console says:
iframe-include.html:11 iframe says: http://127.0.0.1:8181 iframe.html:11 MessageEvent {isTrusted: true, data: 2, origin: "null", lastEventId: "", source: Windowβ¦}
Each document states that it is important to check the event.origin event inside the message event listener. But how to do this if it is always null?
thanks for the help
javascript html5 postmessage
rekam
source share