CKTIS / JavaScript
MessageEvent: source, origin, and data
Distinguish the sender reference, the sender’s origin at send time, and the payload, and learn what to validate before handling a window message.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
event.source
event.origin
event.dataMessageEvent.source/origin/data refers to three read-only instance properties: source, origin, and data. They belong to a web platform API, not ECMAScript 2024; availability depends on the runtime.
For Window.postMessage(), event.source is the WindowProxy of the sender’s browsing context. When a window posts to itself, the sender and receiver have the same WindowProxy. Depending on the API, source can also be a MessagePort (for example, in a shared worker’s connect event), a ServiceWorker, or null. An ordinary message event delivered through a MessagePort has source set to null and origin set to "".
For window messaging, event.origin is the sender’s serialized origin at send time: scheme, host, and port if applicable, with no path, query, or fragment. The same WindowProxy can refer to a different document after navigation, so origin does not tell you what is loaded now. An opaque origin serializes to the string "null", which is distinct from the empty string "" and the JavaScript value null.
event.data is the payload. Window.postMessage() uses structured cloning, not JSON or a direct reference to the original object. Sending a value that cannot be cloned, such as a function, throws DataCloneError on the sending side.
Before acting on data, compare origin with the exact expected origin using ===, check source if you know which window to expect, and validate the payload’s structure and values. When sending or replying, specify an exact targetOrigin if known. It restricts the recipient and does not replace the recipient’s incoming-message checks. Do not send confidential data with "*".
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MessageEvent: source, origin, and data</title>
</head>
<body>
<script>
(() => {
const expectedOrigin = window.location.origin;
const expectedSource = window;
function receive(event) {
if (event.origin !== expectedOrigin || event.source !== expectedSource) return;
const data = event.data;
if (data === null || typeof data !== "object" ||
data.type !== "cktis:count" || !Number.isInteger(data.count)) return;
window.removeEventListener("message", receive);
console.log(`source === window: ${event.source === window}`);
console.log(`origin === expectedOrigin: ${event.origin === expectedOrigin}`);
console.log(`data.count: ${data.count}`);
}
window.addEventListener("message", receive);
window.postMessage({ type: "cktis:count", count: 7 }, expectedOrigin);
})();
</script>
</body>
</html>Result
source === window: true origin === expectedOrigin: true data.count: 7
Serve the HTML file over HTTP(S) and open it in an unsandboxed browser tab. The window sends a message to itself. The three lines appear asynchronously in the browser console after the handler accepts the message and removes its listener.
Related lessons
- JavaScript · Lesson 5 · Advanced · Version: 4