Cross-document messaging and origin checks
Quick answer
Window.postMessage allows deliberate communication between windows or frames, even when direct DOM access is restricted by origin. The sender and receiver must establish a strict message contract.
How it works
Send otherWindow.postMessage(data, targetOrigin) with the exact intended origin (scheme, host and port) for a normal origin. On receipt compare event.origin with the expected origin and event.source with the known window when applicable, then validate event.data. The origin describes the sender at send time, not its current or future location. A wildcard target can expose data after navigation; opaque-origin targets require special handling and should not receive secrets. For local fixture tests, a constructed MessageEvent checks only the decision function, not actual cross-window delivery.
Local examples
Run in the isolated projects/realtime-lab development origin (see README). NOT RUN in local validation; expected result is instructional.
(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));})()Expected console output (not executed locally):
{"kind":"ready"}On the lab origin, self-posting demonstrates queued delivery; a real frame still needs source validation.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const origin="https://app.example";const accept=e=>e.origin===origin&&e.source===window&&e.data?.kind==="ready";const a=new MessageEvent("message",{origin,source:window,data:{kind:"ready"}});const b=new MessageEvent("message",{origin:"https://app.example.invalid",source:window,data:{kind:"ready"}});console.log(accept(a),accept(b));})()Expected console output:
true falseThe local predicate rejects a lookalike origin.
Common mistakes
Do not use substring matching on origins or trust a message because its data contains a trusted-looking sender field. Do not confuse event.origin with targetOrigin.
Practice
Write an exact-origin and source check for a known frame; test a malformed payload and a lookalike origin using synthetic events.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cross-document messaging and origin checks — Knowledge Check
Apply the concept and reason about local example output.
Which property identifies the origin at the time a window message was sent?
How should a known sender origin be compared?
Why check event.source when communicating with a known frame?
What is the expected console output of Cross-document messaging and origin checks example 1?
(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));})()What is the expected console output of Cross-document messaging and origin checks example 2?
(async () => {const origin="https://app.example";const accept=e=>e.origin===origin&&e.source===window&&e.data?.kind==="ready";const a=new MessageEvent("message",{origin,source:window,data:{kind:"ready"}});const b=new MessageEvent("message",{origin:"https://app.example.invalid",source:window,data:{kind:"ready"}});console.log(accept(a),accept(b));})()