Fixing the postMessage target origin error in JavaScript
When you call postMessage and the console shows The target origin provided (…) does not match the recipient window's origin (…), the destination origin you passed as the second argument (targetOrigin) does not match the origin of the page that actually receives the message. In that case, the message is not delivered. Put the recipient's origin, shown in the second pair of parentheses of the message, into targetOrigin. The error goes away and the message arrives.
This error often appears when you exchange data between a parent page and a page on another domain embedded in an iframe, such as a payment form, a video player, or a chat widget. I caused this error on purpose in a test environment, and confirmed the message it produces and that it disappears after the fix.
The test environment was Ubuntu 24.04 and Node.js 22.23.5. I used Playwright 1.63.0 to drive Chromium 157 and open the pages. I took the DevTools screenshots from a headed Chromium running on a virtual display (Xvfb). I prepared a small server that serves the parent page at http://localhost:8080 and the child page, embedded in an iframe, at http://localhost:8081. These two are separate origins that differ only by port. I did not test Firefox, Safari, or sending to a separate window opened with window.open.
The message you get when specifying your own origin
I embedded the child page (localhost:8081) in an iframe on the parent page (localhost:8080). The child page shows the text it receives in a message event on the screen and sends a reply back to the sender.
<!-- Excerpt of the child page (http://localhost:8081/child.html). The console.log calls used for checking are omitted -->
<p id="got">まだ何も受け取っていません</p>
<script>
window.addEventListener('message', (event) => {
// Check the sender's origin before using the data
if (event.origin !== 'http://localhost:8080') return;
document.getElementById('got').textContent = `受け取りました:${event.data}`;
// Reply, specifying the sender's origin as the target origin
event.source.postMessage('子から返事です', event.origin);
});
</script>
After the iframe finishes loading, the parent page sends a "hello" message with postMessage. Here, I wrote the parent's own origin as the second argument, not the child's origin. The reply from the child is received and shown by a message event listener in a separate file (common.js).
<!-- Excerpt of the parent page (http://localhost:8080/wrong-origin.html) -->
<iframe id="child" src="http://localhost:8081/child.html"></iframe>
<script>
// Wrong: specifies the parent's own origin instead of the recipient's (child's) origin
const iframe = document.getElementById('child');
iframe.addEventListener('load', () => {
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8080');
});
</script>
When I opened this page, the following message appeared in the DevTools console.
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:8080') does not match the recipient window's origin ('http://localhost:8081').
It means "the target origin provided (http://localhost:8080) does not match the origin of the receiving window (http://localhost:8081)." The child page still showed its initial message, "nothing received yet", and no reply reached the parent.

In the console, this line appeared as a yellow warning, not a red error. When a message does not arrive, check warning lines as well as error lines.
The MDN description of postMessage says that for a message to be delivered, the origin must match exactly, including scheme, hostname, and port. In this test, the scheme and hostname were the same, and only the port (8080 vs. 8081) differed. Even that port-only difference made them separate origins, and the message was not delivered.
Specifying the recipient's origin removes the error
The second pair of parentheses (recipient window's origin) in the message shows the origin of the page that actually receives the message. I changed the second argument on the parent page to the child's origin, http://localhost:8081.
<!-- Excerpt of the parent page (http://localhost:8080/fixed.html) -->
<iframe id="child" src="http://localhost:8081/child.html"></iframe>
<script>
// Fixed: send after the child page has loaded, specifying the child's origin
const iframe = document.getElementById('child');
iframe.addEventListener('load', () => {
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8081');
});
</script>
When I opened the page, there were no errors or warnings in the console. The child page showed "Received: hello", and the parent page showed "Reply: the child's reply". The two lines in the console come from console.log calls on each page, which I added to confirm that the messages were received.

Replies from the child to the parent work the same way. The child page passes the event.origin of the received event (the sender's origin) as the second argument, so there is no chance of mistyping the parent's origin.
Forms that give the same warning and forms that don't
I tried other targetOrigin forms to see when delivery fails.
When the hostname is written differently, the same warning appears and the message is not delivered. The child is opened as http://localhost:8081, but when I specified http://127.0.0.1:8081, the output was:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://127.0.0.1:8081') does not match the recipient window's origin ('http://localhost:8081').
Both point to your own computer, but localhost and 127.0.0.1 are treated as different origins, so the message was not delivered. Make the hostname in targetOrigin match the URL you wrote in the iframe's src.
On the other hand, the following two were delivered, the same as when I wrote http://localhost:8081.
// Also delivered when the URL includes a path
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8081/child.html');
// Also delivered with a trailing "/"
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8081/');
Only the origin (scheme, hostname, and port) is compared in targetOrigin, and the path makes no difference. Passing the same URL as the iframe's src is fine.
'*' also delivers the message. However, the MDN security considerations recommend specifying a concrete origin rather than * when you know where the recipient is. If the iframe's content were replaced with a malicious site, * would hand your data to that site. Don't change the value to * just to silence the warning. Write the correct origin instead.
Omitting "http://" gives a different error
When I wrote targetOrigin without a scheme, as 'localhost:8081', a different error appeared.
SyntaxError: Failed to execute 'postMessage' on 'Window': Invalid target origin 'localhost:8081' in a call to 'postMessage'.
The lines above show the exception name and message.
It means "the target origin passed to postMessage is not valid." This is not a warning. postMessage throws a SyntaxError exception, and nothing reached the child page. Write targetOrigin in a form that starts with http:// or https://.
The origin-mismatch warning is not an exception. When I wrapped postMessage in try...catch, the catch block did not run. The console.log on the next line ran as usual, and the same warning was printed.
// Excerpt (runs inside the iframe's load event)
try {
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8080');
console.log('postMessage は例外を投げずに戻った');
} catch (e) {
console.log('catch に入った', e.name);
}
postMessage は例外を投げずに戻った
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:8080') does not match the recipient window's origin ('http://localhost:8081').
try...catch cannot tell you that a message was not delivered. Confirm delivery with a reply, or check the console warning.
If the origin is correct but you still get the warning, you are sending too early
Even when targetOrigin is correct, the second pair of parentheses may show the parent page's own origin. I tested this with a child page that waits one second before responding, and a parent page that sends right after placing the iframe, without waiting for load.
<iframe id="child" src="http://localhost:8081/child.html?delay=1000"></iframe>
<script>
// Wrong: sends before the child page has finished loading
const iframe = document.getElementById('child');
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8081');
</script>
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:8081') does not match the recipient window's origin ('http://localhost:8080').
The receiving origin was the parent (8080), not the child (8081). This means the iframe did not yet contain the child page. Nothing reached the child page.
With the same child page that waits one second, I waited for the iframe's load event before sending, as in the first fix. No warning appeared, and both the message and the reply were delivered.
// Fixed: send after the child page has finished loading
iframe.addEventListener('load', () => {
iframe.contentWindow.postMessage('こんにちは', 'http://localhost:8081');
});
I also changed the child page to respond without any delay, and opened the page that sends without waiting for load five times. Four times the message arrived. Once it did not arrive, and no warning was shown. Because timing can cause silent failures like this, wait for load before sending to an iframe.





