Tom Gullen
Tom Gullen

Reputation: 61773

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

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?

Upvotes: 8

Views: 28982

Answers (2)

Axel Osorio
Axel Osorio

Reputation: 39

I got this error when I run the code locally (in a local directory), but when I put it in a webserver (Tomcat), the code works.

Upvotes: 3

Tom Gullen
Tom Gullen

Reputation: 61773

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

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

Upvotes: 5

Related Questions