25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

In this example, we show how a portal might embed widgets from third parties in iframes. Our<br />

example shows a single widget from chat.example.net. The portal page and widget then communicate<br />

using postMessage. In this case, the iframe represents a chat widget that wants to notify the user by<br />

blinking the title text. This is a common UI technique found in applications that receive events in the<br />

background. However, because the widget is isolated in an iframe served from a different origin than the<br />

parent page, changing the title would be a security violation. Instead, the widget uses postMessage to<br />

request that the parent page perform the notification on its behalf.<br />

The example portal also sends messages to the iframe to inform the widget that the user has<br />

changed his or her status. Using postMessage in this way allows a portal such as this to coordinate with<br />

widgets across the combined application. Of course, because the target origin is checked when the<br />

message is sent, and the event origin is checked when it is received, there is no chance that data leaks<br />

out accidentally or is spoofed.<br />

■ Note In this example application, the chat widget is not connected to a live chat system, and notifications are<br />

driven by the application’s users clicking Send Notification. A working chat application could use Web Sockets,<br />

as described in Chapter 6.<br />

For the sake of illustration, we created a few simple <strong>HTML</strong> pages: postMessagePortal.html and<br />

postMessageWidget.html. The following steps highlight the important parts of building the portal page<br />

and the chat widget page. The sample code for the following examples is located in the<br />

code/communication folder.<br />

Building the Portal Page<br />

First, add the chat widget iframe hosted at the different origin:<br />

<br />

Next, add an event listener messageHandler to listen for message events coming from the chat<br />

widget. As shown in the following example code, the widget will ask the portal to notify the user, which<br />

can be done by flashing the title. To make sure the message comes from the chat widget, the message’s<br />

origin is verified; if it does not come from http://chat.example.net:9999, the portal page simply ignores<br />

it.<br />

var targetOrigin = "http://chat.example.net:9999";<br />

function messageHandler(e) {<br />

if (e.origin == targetOrigin) {<br />

notify(e.data);<br />

} else {<br />

// ignore messages from other origins<br />

}<br />

}<br />

121

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!