postMessage: the target origin does not match the recipient window

Your script sends a message to a frame with a target origin, but the page in the frame has another origin. The browser drops the message and writes this warning.

  • Chrome
  • Edge
  • Firefox
  • Safari

Updated

Console error · Chrome and Edge, exact text

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

Chrome, EdgeExact text

The message never arrives. Chrome writes a warning, not an error.

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

FirefoxExact text

The message never arrives.

Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘https://example.com’) does not match the recipient window’s origin (‘https://www.example.com’).

SafariExact text

The message never arrives.

Unable to post message to https://example.com. Recipient has origin https://www.example.com.

On this page

What it means

The browser checks the target origin of each message against the page that is in the frame now. They differ, so it drops the message. Nothing reaches the other page, and no event fires there.

Why it happens

The second argument of postMessage is the origin that may get the message:

iframe.contentWindow.postMessage(data, "https://example.com");
  1. The frame redirected to another host, such as www.example.com, or to http from https.

  2. The frame has not loaded yet. It still holds the first empty page, which has the origin of your page. Send the message after the load event.

  3. A path in the target is ignored. Only the scheme, the host and the port count.

Check the page

See the headers the page sends now

Fix it if you own the site

Use the exact origin of the page in the frame as the target. Wait for the load event of the iframe before you send. The postMessage guide has the full pattern.

Your page
iframe.addEventListener("load", () => {
  iframe.contentWindow.postMessage({ type: "hello" }, "https://www.example.com");
});

Do not use "*" as the target origin for private data. Any page that loads in the frame would get it.

If you do not own the site

This message comes from your own script, so you can fix it. If the other page sends it, use one of these.

  1. Tell the owner the right origin

    Send them the origin of your page, with the scheme and any port.

  2. Check the frame URL

    Load the final URL of the page, after its redirects, in your iframe.

Sources

Know before you embed.

Free, no sign-up, nothing stored. Paste a URL and get a verdict, a live preview, and the fix.