Cross domain iframe communication using window.postMessage

In HTML5 we have methods, window.postMessage and window.receiveMessage, which can be use to pass messages to different frames in a HTML document.

These two frame can belong to two different domains as well.

To send a message to a window, we will have to use window.postMessage

Lets say this frame is from url and trying to post a message to another frame
And this frame is opened as an iframe in document.


The above code will pass a string "Hello" to frame (, note we have used top instead of parent.

and top frame need an event to catch this message as well.

window.addEventListener("message", receiveMessage, false);  

function receiveMessage(event)
    if (event.origin !== "") //checking for the origin of the message
    if( == 'Hello') //check for the message
        alert("Hello from");

In a similar way we can send message from parent frame to child frame.
Add Comments :