HTML5 postMessage() The API method has the syntax as shown below:
userWindow.postmessage(myMessage, targetOrigin);
this will send myMessage to the window specified by userWindow , which has targetOrigin as a URI. If the userWindow link matches, but targetOrigin does not match the URI, then the message will not be sent.
In the target window, i.e. userWindow , you can listen to the message event.
window.addEventListener('message', handlerFunction, captureBubble);
For example, if you have a link to a window in the myWindow variable, then on the source ...
Call
myWindow.postMessage('this is a message', 'http://www.otherdomain.com/');
Callback handling
window.addEventListener("message", receiveMessage, false); function receiveMessage(event){ if (event.origin !== 'http://www.otherdomain.com/') return; // this check is neccessary // because the `message` handler accepts messages from any URI console.log('received response: ',event.data); }
and on target ...
Message handler
window.addEventListener("message", receiveMessage, false); function receiveMessage(event){ if (event.origin !== 'http://www.callingdomain.com/') return; console.log('received message: ',event.data); event.source.postMessage('this is response.',event.origin); }
postMessage API Link - MDN
Good tutorial