Web APIs

Window.prototype.postMessage()

Try the free JavaScript compiler

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

NameTypeDescription
messageanyStructured-cloneable data.
targetOriginstringExact intended recipient origin.
transferobject[]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));})()
JavaScript Reference