Methods
emitPostMessage(target, origin, messageType, payloadopt, nullable)
Emit/dispatch a post message on a valid target, like a window or an iframe.
Putting the origin as an obligatory parameter at the second place, is deliberate by design, to force everyone to really think about, what to use here. Usually, most people, just throw in the "*" wildcard, paying no attention to the security implications. Please really think about what to use here.
This function adds the messageType
automatically to the message/payload using the key type
. onPostMessage
will
use that information additionally to the origin
to determine if a registration fits the occurred event. The
payload
will be placed in the message using the key payload
. So e.data
will look like this in the
handler at the end: {type : messageType, payload : {...payload}}
A word of advice: keep in mind, that, contrary to most other events in javascript, post messages actually work asynchronously (so you cannot be sure, that the handler has been executed, directly after a post message has been sent) and that messages/payload are not transferred as-is, but are cloned, using the "structured clone algorithm", which means, that not every javascript object is transferable without losses.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Window | HTMLIFrameElement | window/iframe to receive the post message (iframes are automatically resolved to the contentWindow) |
||
origin |
String | the origin the current context has to have, to actually send the post message the received post message has to have, this does NOT set the origin! (defaults to "*", if receiving a nullish value) |
||
messageType |
String | the type/name of the post message (will be checked using the key "type" in the message's payload, which will automatically be set using this function) |
||
payload |
* |
<optional> <nullable> |
null | a payload to add to the message under the key "payload" |
- Source:
- See:
Throws:
error if target is not usable
Returns:
Example
emitPostMessage(window, '*', 'foobar-message', {timestamp : new Date()});
emitPostMessage(iframeElement, 'https://foobar.com:80/', 'foobar-message');