iframe Window
This example demonstrates two-way communication between a parent window and an embedded iframe using the HTML5 `postMessage` API. The parent page creates an iframe dynamically and sends random numeric strings to it via `iframeEl.contentWindow.postMessage`, while the iframe sends messages back using `window.parent.postMessage`. Both pages listen for incoming messages with `window.addEventListener('message')` and display the received data in a results div. The iframe is styled with a red background and includes a button to trigger outgoing messages.
AI-generated descriptionTwo way iframe communication
The main difference between the two pages is the method of sending messages. Recieving messages is the same in both.
Parent
Send messages to iframe using iframeEl.contentWindow.postMessage
Recieve messages using window.addEventListener('message')
iframe
Send messages to parent window using window.parent.postMessage
Recieve messages using window.addEventListener('message')