Cross-domain message-posting demo

The red box below contains an iframe whose contents were loaded from a different domain. When you click on the button, the parent frame sends a message to its child frame via the window.postMessage method. The child frame prints the message and then sends another message back to the parent frame, which prints that message.

The window.postMessage method and its associated events are explained very well here, and browser support for them is detailed here (make sure you click on "Show all versions" in the upper left corner of the table).

In general, you should be all set unless you have to support IE 7 or lower. If window.postMessage is not working for you, check out all the older ways to do cross-domain message-passing here.

Feel free to view the source of both of the frames on this page. The JavaScript code is contained within the html files.

After the child received a message, it sent the following response back to the parent: