Web APIs
Window.prototype.postMessage()
Queues delivery to a window if its origin matches the target restriction. Use an exact target origin for ordinary origins.
Formal Syntax & Structure
javascript syntaxjavascript
targetWindow.postMessage(message, targetOrigin, [transfer])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| message | any | Structured-cloneable data. |
| targetOrigin | string | Exact intended recipient origin. |
| transfer | object[] | Optional transferable objects. |
Return Value
undefined
Queues delivery to a window if its origin matches the target restriction. Use an exact target origin for ordinary origins.
Working Example
Example Snippetjavascript
(async () => {const message={kind:"ready"};const received=new Promise(ok=>window.addEventListener("message",e=>ok(e.data),{once:true}));window.postMessage(message,location.origin);console.log(JSON.stringify(await received));})()