Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
HTML5 Zone is brought to you in partnership with:

Robert has been working with web developing, mostly interface coding, since 1998. His biggest interests lies in HTML, CSS and JavaScript, where especially JavaScript has been a love for quite some time. He regularly blogs at http://www.robertnyman.com about web developing, and is running/partaking in a number of open source projects (http://code.google.com/u/robnyman/). Robert is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

postMessage in HTML5 to send messages between windows and iframes

10.03.2011
Email
Views: 2631
  • submit to reddit
The HTML5 Microzone is presented by DZone and Microsoft to bring you the most interesting and relevant content on emerging web standards.  Experience all that the HTML5 Microzone has to offer on our homepage and check out the cutting edge web development tutorials on Script Junkie, Build My Pinned Site, and the HTML5 DevCenter.

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:

  1. Call the postMessage method of the window/iframe element you want to send the information to.
  2. Specify origin. Should be a domain or a wildcard "*"
  3. In the receiving window, just add an event listener for the message event.
  4. 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! :-)





Published at DZone with permission of Robert Nyman, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

HTML5 is the most dramatic step in the evolution of web standards. It incorporates features such as geolocation, video playback and drag-and-drop. HTML5 allows developers to create rich internet applications without the need for third party APIs and browser plug-ins.  Under the banner of HTML5, modern web standards such as CSS3, SVG, XHR2, WebSockets, IndexedDB, and AppCache are pushing the boundaries for what a browser can achieve using web standards.  This Microzone is supported by Microsoft, and it will delve into the intricacies of using these new web technologies and teach you how to make your websites compatible with all of the modern browsers.

Comments

Rick Ross replied on Tue, 2011/10/04 - 3:50pm

Very cool technique, Robert. How widely implemented is this particular aspect of the preliminary HTML5 specs?

Zqudlyba Navis replied on Mon, 2011/12/05 - 5:40pm

Can anyone give a working example in GWT?

Carla Brian replied on Mon, 2012/04/16 - 10:50am

I love using the HTML 5 though im still new to this. It is somehow same with the CSS code. - Paul Perito MD

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.