Use postmessage to report, Failed to execute 'postMessage' on' DOMWindow'

use postMessage to send cross-domain messages,
window.postMessage ("hello there messages, location.protocol +" / a.b.com") ;

but the following error was reported:

Failed to execute "postMessage" on "DOMWindow": The target origin provided ("http://a.b.com") does not match the recipient window"s origin ("http://c.e.com")

what is the reason, please?

Mar.23,2021

use event.source as reply object
for more information, please see https://developer.mozilla.org.

.
//ApostMessagefunctionaddEventListenner
function receiveMessage(event)
{
  // 
  if (event.origin !== "http://example.com:8080")
    return;

  // event.source 
  // event.data  "hello there!"

  // origin (), enent.source
  // event.origintargetOrigin
  event.source.postMessage("hi there yourself!  the secret response " +
                           "is: rheeeeet!",
                           event.origin);
}

window.addEventListener("message", receiveMessage, false);

otherWindow.postMessage (message, targetOrigin, [transfer]);
otherWindow: A reference to other windows , such as the contentWindow property of iframe, the window object returned by window.open, or a named or numerically indexed window.frames.
otherWindow cannot be the current window.
see MDN

.
Menu