Failed to execute 'postMessage' on 'Window': Invalid target origin

Tom Gullen picture Tom Gullen · Apr 28, 2015 · Viewed 23.2k times · Source

I have the iframe:

<iframe id="GameFrame" 
sandbox="allow-scripts allow-pointer-lock" 
src="https://127.0.0.1:112/games/1047/play">
</iframe>

My parent page is located at:

https://127.0.0.1/arcade/not-hidden/space-blaster-1047

I'm trying to post a message to the iFrame:

var gameIframe = $("#GameFrame");
gameIframe.get(0).contentWindow.postMessage("screenshot", "");

But this throws the error:

Uncaught SyntaxError: Failed to execute 'postMessage' on 'Window': Invalid target origin '' in a call to 'postMessage'.

Other attempts:

postMessage("screenshot", "https://127.0.0.1");

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://127.0.0.1') does not match the recipient window's origin ('null').

How can I get this posting a message to the iFrame?

Answer

Tom Gullen picture Tom Gullen · Apr 28, 2015

Just figured this out right now, need to use * as the origin:

gameIframe.get(0).contentWindow.postMessage("screenshot", "*");