Frames talk through window.postMessage(message, targetOrigin), which works across origins. The sender gets a reference to the other window (iframe.contentWindow, window.parent, window.top, window.opener), posts a structured-cloneable message, and the receiver listens for the message event. Always pass an exact targetOrigin and verify event.origin on receipt. For high-volume or request/response traffic, transfer a MessageChannel port. Same-origin frames can also access each other's DOM directly or use BroadcastChannel.
// parent page, https://app.example.com
const frame = document.querySelector('iframe#widget');
frame.addEventListener('load', () => {
frame.contentWindow.postMessage(
{ type: 'init', theme: 'dark' },
'https://widget.example.net' // only deliver if the frame is still this origin
);
});
// inside https://widget.example.net
const PARENT_ORIGIN = 'https://app.example.com';
window.addEventListener('message', (event) => {
if (event.origin !== PARENT_ORIGIN) return; // ignore everyone else
if (event.data?.type === 'init') applyTheme(event.data.theme);
});
window.parent.postMessage({ type: 'resize', height: document.body.scrollHeight }, PARENT_ORIGIN);
event.source is the sender's window, so you can reply with event.source.postMessage(…, event.origin).
// parent
const { port1, port2 } = new MessageChannel();
port1.onmessage = (e) => console.log('reply', e.data);
frame.contentWindow.postMessage({ type: 'connect' }, 'https://widget.example.net', [port2]);
// iframe
window.addEventListener('message', (e) => {
if (e.origin !== PARENT_ORIGIN || e.data.type !== 'connect') return;
const port = e.ports[0];
port.onmessage = (m) => port.postMessage({ ok: true, echo: m.data });
});
A dedicated port avoids filtering every message event on window and gives you a private channel after a single origin check. Libraries like Comlink or Penpal build RPC on top of this.
'*' as targetOrigin for sensitive data. If the frame navigated to another site, * would leak it.event.origin with an exact match (not includes or endsWith('example.com'), which evilexample.com passes).event.data as untrusted input. Validate its shape, never eval it or inject it as HTML.sandbox attribute on untrusted iframes (sandbox="allow-scripts" without allow-same-origin gives it an opaque origin), and allow (Permissions Policy) to restrict features.frame-ancestors in CSP.| Method | When |
|---|---|
Direct DOM access (iframe.contentDocument) | Same-origin only |
BroadcastChannel('name') | Same-origin frames/tabs, pub/sub |
localStorage + storage event | Same-origin tabs (legacy pattern) |
| URL fragment changes | Old cross-origin hack, avoid |
Data is copied with the structured clone algorithm: functions and DOM nodes can't be sent, while ArrayBuffers and ports can be transferred (zero-copy).
postMessage + message event, with exact targetOrigin and event.origin checks.MessageChannel for request/response, and RPC wrappers.Spotted a mistake or a rights concern? Report an issue