Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

Blocked a frame with origin from accessing a cross-origin frame — how can I communicate safely?

Illustration without a description Illustration without a description

The parent and iframe have different origins, so the browser blocks direct DOM access between them. Compare scheme, host and port if the difference is surprising. Pages can belong to the same organization and still be separated by the browser’s same-origin policy.

 

Use postMessage() for the information the frames deliberately share. Set an explicit target origin. In the receiving handler, check the origin, the expected frame window and the message shape before acting. Define the message that each side is allowed to send.

 

The parent example waits for READY from the expected partner iframe. In a test, send a valid READY from that frame and another from a different same-origin frame. Only the expected iframe should reach the handler.

 

// Parent page. Register the listener before the partner sends READY. const partner = document.querySelector('iframe#partner'); const partnerOrigin = 'https://app.example.com'; window.addEventListener('message', event => { if (!partner?.contentWindow) return; if (event.origin !== partnerOrigin || event.source !== partner.contentWindow) return; const data = event.data; if (!data || typeof data !== 'object' || Array.isArray(data)) return; if (data.type !== 'READY') return; console.log('Partner is ready'); });

 

Send a known message between the frames and verify the receiver ignores messages from unexpected origins or with the wrong shape.