CKTIS / JavaScript
window.postMessage — messaging between windows
window.postMessage asynchronously sends data to a target window, including across origins. Prefer an exact `targetOrigin`; omitting it restricts delivery to the same origin. Receivers should validate the sender's origin, the sending window, and the payload structure.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
targetWindow.postMessage(message, targetOrigin[, transfer])
targetWindow.postMessage(message[, { targetOrigin, transfer }])window.postMessage is a browser API defined by the HTML Living Standard, not a feature of a particular ECMAScript edition. Current browser engines support it; Node 24 in a repository does not establish its availability. Call the method on the receiving window, such as iframe.contentWindow or a reference to an opened window. Messaging does not grant access to another origin's DOM.
The receiver gets an asynchronous message event: event.data contains the payload, event.origin contains the serialized origin of the sender at the time of sending, and event.source references the sending window. A tuple origin is identified by its scheme, host, and port, not its path. An opaque origin has no such tuple and serializes to the string "null". Checking that string alone does not distinguish one opaque origin from another. Register the listener before sending; coordinate receiver readiness for a newly created frame.
Specify an exact targetOrigin. A mismatch with the receiver's origin causes the message to be discarded. In the options-object form, omitting targetOrigin defaults to "/", restricting delivery to the same origin. "*" allows any origin—do not use it for confidential data. Receivers should validate event.origin, event.source, and the payload structure; never treat messages as code.
Data uses structured cloning, not JSON serialization. Functions cannot be cloned. The transfer list moves resources instead of copying them: a transferred ArrayBuffer becomes detached on the sending side. Uncloneable data or duplicate transfer entries can cause DataCloneError. In the example, a sandboxed iframe sends a message to its parent window using window.parent.postMessage. The parent checks that event.origin is "null" and that event.source matches the iframe's window. It then validates the payload and logs received: 42.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Send a message from an iframe</title>
</head>
<body>
<script>
const frame = document.createElement('iframe');
frame.title = 'Message sender';
frame.hidden = true;
frame.setAttribute('sandbox', 'allow-scripts');
// Use the page's exact origin when it has an HTTP(S) origin.
// The wildcard fallback carries only the fixed, non-sensitive fixture.
const targetOrigin = /^https?:\/\//.test(window.origin) ? window.origin : '*';
function receive(event) {
// The sandboxed sender has an opaque origin, serialized as "null".
if (event.origin !== 'null' || event.source !== frame.contentWindow) return;
const data = event.data;
if (data === null || typeof data !== 'object' || Array.isArray(data)) return;
if (data.type !== 'cktis-demo' || data.value !== 42) return;
window.removeEventListener('message', receive);
console.log(`received: ${data.value}`);
}
window.addEventListener('message', receive);
frame.srcdoc = `<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Message sender</title></head>
<body>
<script>
window.parent.postMessage({ type: 'cktis-demo', value: 42 }, ${JSON.stringify(targetOrigin)});
<\/script>
</body>
</html>`;
document.body.append(frame);
</script>
</body>
</html>Result
received: 42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.