postMessage in HTML5 to send messages between windows and iframes
Ever had the need to communicate between windows or the current window and an inner iframe? Across domains as well? I bet you have, but now we have a nice option for doing that!
The solution is called postMessage and is part of the HTML5 Web Messaging specification. What makes it cool, and very easy to use, is that all you need to trigger it is to call a method and add an event handler:
- Call the
postMessagemethod of the window/iframe element you want to send the information to. - Specify origin. Should be a domain or a wildcard
"*" - In the receiving window, just add an event listener for the
message event. - Optional: add an origin check for security reasons.
The code to do this
In this example we will have one containing page with a form, posting a message to an iframe in that page.
Code used in the containing page
<form id="the-form" action="/"> <input type="text" id="my-message" value="Your message"> <input type="submit" value="postMessage"> </form> <iframe id="da-iframe" src="postMessageTarget.html"></iframe>
window.onload = function () {
var iframeWin = document.getElementById("da-iframe").contentWindow,
form = document.getElementById("the-form"),
myMessage = document.getElementById("my-message");
myMessage.select();
form.onsubmit = function () {
iframeWin.postMessage(myMessage.value, "http://robertnyman.com");
return false;
};
};
Code used in the iframe
<p id="received-message">I've heard nothing yet</p>
function displayMessage (evt) {
var message;
if (evt.origin !== "http://robertnyman.com") {
message = "You are not worthy";
}
else {
message = "I got " + evt.data + " from " + evt.origin;
}
document.getElementById("received-message").innerHTML = message;
}
if (window.addEventListener) {
// For standards-compliant web browsers
window.addEventListener("message", displayMessage, false);
}
else {
window.attachEvent("onmessage", displayMessage);
}
Web browser support
The nice thing is that this is supported in:
- Internet Explorer 8.0+
- Firefox 3.0+
- Safari 4.0+
- Google Chrome 1.0+
- Opera 9.5+
Demo and my HTML5 playground
You can see the above code in action at my postMessage demo, which is part of a new testing ground on my web site, HTML5 – Information and samples for HTML5 and related APIs which displays various HTML5 examples, the code to run them and web browser compatibility..
Play around and enjoy!

17 Comments/Reactions
March 18th, 2010 at 11:07
Cool! You should check out pmrpc – http://code.google.com/p/pmrpc/ – a HTML5 JavaScript library for RPC-style (remote procedure call) inter-window and web workers communication. The library is based on postMessage (both the inter-window API and worker API).
April 1st, 2010 at 15:38
It seems like the support in IE8 is somewhat limited, it only works in frames/iframes and have some issues with both session and local storage. See Eric Lawrence’s blog http://bit.ly/HwrlP
April 3rd, 2010 at 0:01
easyXDM (http://easyxdm.net/) is also a library using postMessage (and several fallback techniques) for supporting cross-domain RPC, just like pmrpc, but easyXDM has more advanced functionality and to be honest, better code structure and documentation.
April 5th, 2010 at 8:57
Ivan,
Thanks for the tip!
Henrik,
Ah, good to know! Thanks!
Sean,
Good, great to have alternatives!
Write a comment
Twitter reactions
March 18th, 2010 at 10:33
postMessage in HTML5 to send messages between windows and iframes: http://bit.ly/an7hH3
This comment was originally posted on Twitter
March 18th, 2010 at 10:35
Some #html5 reading http://is.gd/aN5om http://is.gd/aN5oP http://is.gd/aN5ph
This comment was originally posted on Twitter
March 18th, 2010 at 10:49
RT @robertnyman postMessage in HTML5 to send messages between windows and iframes: http://bit.ly/an7hH3
This comment was originally posted on Twitter
March 18th, 2010 at 10:53
RT @robertnyman: postMessage in HTML5 to send messages between windows and iframes: http://bit.ly/an7hH3
This comment was originally posted on Twitter
March 18th, 2010 at 11:25
postMessage in HTML5 to send messages between windows and iframes: http://bit.ly/an7hH3 (via @robertnyman)
This comment was originally posted on Twitter
March 18th, 2010 at 11:30
postMessage in HTML5 to send messages between windows and iframes: http://bit.ly/an7hH3 (via @robertnyman) (via @html5patch)
This comment was originally posted on Twitter
March 18th, 2010 at 12:52
RT postMessage in HTML5 to send messages between windows and iframes: Ever had the need to communicate between win… http://bit.ly/aJfV5u
This comment was originally posted on Twitter
March 18th, 2010 at 13:26
postMessage in #HTML5 to send messages between windows and iframes http://tinyurl.com/yebmbnp /by @robertnyman
This comment was originally posted on Twitter
March 18th, 2010 at 16:37
postMessage in HTML5 to send messages between windows and iframes – Robert’s talk http://ff.im/-hJIte
This comment was originally posted on Twitter
March 19th, 2010 at 3:40
postMessage in #HTML5 to send messages between windows and iframes http://short.to/1purd
This comment was originally posted on Twitter
March 19th, 2010 at 4:45
RT @purplehayz: postMessage in #HTML5 to send messages between windows and iframes http://short.to/1purd
This comment was originally posted on Twitter
March 19th, 2010 at 20:17
postMessage In HTML5 To Send Messages Between Windows And Iframes /by @robertnyman via @pchristoph http://j.mp/cVQnlj #html5
This comment was originally posted on Twitter
March 20th, 2010 at 19:00
postMessage in HTML5 to send messages between windows and iframes.. http://bit.ly/9Oe0U7
This comment was originally posted on Twitter