Window: postMessage() method
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thewindow.postMessage()
method safely enables cross-origin communication betweenWindow
objects;e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.
Normally, scripts on different pages are allowed to access each other if and only if the pages they originate from share the sameorigin (also known as the "same-origin policy").window.postMessage()
provides a controlled mechanism to securely circumvent this restriction (if used properly).
Furthermore, an accessing script must have obtained the window object of the accessed document beforehand. This can occur through methods such aswindow.open()
for popups oriframe.contentWindow
for iframes.
Broadly, one window may obtain a reference to another (e.g., viatargetWindow = window.opener
), and then dispatch aMessageEvent
on it withtargetWindow.postMessage()
. The receiving window is then free tohandle this event as needed. The arguments passed towindow.postMessage()
(i.e., the "message") areexposed to the receiving window through the event object.
Syntax
postMessage(message)postMessage(message, targetOrigin)postMessage(message, targetOrigin, transfer)postMessage(message, options)
Parameters
message
Data to be dispatched to the other window. The data is serialized using thestructured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself.
targetOrigin
OptionalSpecifies theorigin the recipient window must have in order to receive the event. In order for the event to be dispatched, the origin must match exactly (including scheme, hostname, and port). If omitted, then defaults to the origin that is calling the method. This mechanism provides control over where messages are sent; for example, if
postMessage()
was used to transmit a password, it would be absolutely critical that this argument be a URI whose origin is the same as the intended receiver of the message containing the password, to prevent interception of the password by a malicious third party.*
may also be provided, which means the message can be dispatched to a listener with any origin.Note:Always provide a specific
targetOrigin
, not*
, if you know where the other window's document should be located. Failing to provide a specific target could disclose data to a malicious site.transfer
OptionalAn optionalarray oftransferable objects to transfer ownership of. The ownership of these objects is given to the destination side and they are no longer usable on the sending side. These transferable objects should be attached to the message; otherwise they would be moved but not actually accessible on the receiving end.
options
OptionalAn optional object containing the following properties:
transfer
OptionalHas the same meaning as the
transfer
parameter.targetOrigin
OptionalHas the same meaning as the
targetOrigin
parameter.
Return value
None (undefined
).
The dispatched event
Awindow
can listen for dispatched messages by executing the following #"message", (event) => { if (event.origin !== "http://example.org:8080") return; // … }, false,);
The properties of the dispatched message are:
data
The object passed from the other window.
origin
Theorigin of the window that sent the message at the time
postMessage
was called. This string is the concatenation of the protocol and "://", the host name if one exists, and ":" followed by a port number if a port is present and differs from the default port for the given protocol. Examples of typical origins arehttps://example.org
(implying port443
),http://example.net
(implying port80
), andhttp://example.com:8080
. Note that this origin isnot guaranteed to be the current or future origin of that window, which might have been navigated to a different location sincepostMessage
was called.source
A reference to the
window
object that sent the message; you can use this to establish two-way communication between two windows with different origins.