In the iframe you’ll use the below code:
1 |
And in the parent window:
1 2 3 4 5 6 7 8 9 10 | // function that will be called when the message is received function receiveMessage(event) { alert(event.data); // it'll show "Hello World !" } // we listen to a message if (window.addEventListener) { window.addEventListener('message ', receiveMessage, false); } else if (window.attachEvent) { // IE8 window.attachEvent(' onmessage', receiveMessage); } |
It’s working from IE8+, and all modern browsers, but make sure you have a HTML5 doctype (<!DOCTYPE html>)