Introduction
Cross-domain communication (also known as cross-origin communication) can be complex and dangerous. However, HTML5 has a helpful and sometimes ignored feature called window.postMessage(), which is safe when used correctly.
The window.postMessage() method allows cross-origin communication between Window objects, such as between a page and a pop-up it created or a page and an iframe it contains.
Scripts on different pages usually are allowed to communicate with one another if and only if the pages they come from have the same protocol, port number, and hostname (also known as the "same-origin policy"). window.postMessage() provides a secure way to get around this restriction in a controlled manner.
In general, one window can receive a reference to another (e.g., via targetWindow = window.opener) and then use targetWindow.postMessage to send a MessageEvent() to it. The receiving window is, therefore, free to deal with the situation. The event object exposes the arguments supplied to window.postMessage() (i.e., the "message") to the receiving window.
Let's learn more about the targetWindow,
The window that will receive the message is called the targetWindow. Obtaining such a reference can be done in a variety of ways, including
Components
The following are the two necessary components:
- window.postMessage(): to send message
- window.addEventListener(“message”, callback): to receive and process the message.
Syntax
postMessage(message, targetOrigin)
postMessage(message, targetOrigin, [transfer])
Arguments
message
The information is to be transmitted to the other window. The structured clone algorithm is used to encode the data. Functions cannot be sent as messages. The structured clone algorithm does not allow functions. However, you can send a wide range of data items to the target window without having to encode them beforehand.
targetOrigin
Specifies the origin of targetWindow, either as the literal string "*" or as a URI. It is the URI of the receiver’s page.
If the scheme, hostname, or port of targetWindow's document do not match those supplied in targetOrigin when the event is scheduled to be sent, the event will not be dispatched. Only if all of the three matches, the event will be dispatched.
This mechanism allows you to control where messages are sent. For example, suppose postMessage() is used to send a password. In that case, this argument must be a URI with the exact origin as the intended receiver of the password-containing message to prevent the password from being intercepted by a malicious third party.
Always provide a targetOrigin. If you know where the receiver's domain is, provide it rather than *. If you don't specify a target, the data you send will be visible to any malicious site that is interested.
Note: If “*” is used as the targetOrigin, the message could be from anyone.
For example, if we want to send a message from https://www.codingninjas.com to https://campus.codingninjas.com, the targetOrigin would be https://campus.codingninjas.com, and the recipient’s domain would be https://campus.codingninjas.com as well.
transfer
It is a set of transferable objects that are sent together with the message. These objects are now owned by the destination side and are no longer useable by the sender side.