25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

120<br />

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

function messageHandler(e) {<br />

if(checkWhiteList(e.origin)) {<br />

processMessage(e.data);<br />

} else {<br />

// ignore messages from unrecognized origins<br />

}<br />

}<br />

window.addEventListener(“message”, messageHandler, true);<br />

■ Note The MessageEvent interface defined by <strong>HTML</strong>5 is also part of <strong>HTML</strong>5 WebSockets and <strong>HTML</strong>5 Web<br />

Workers. The communication features of <strong>HTML</strong>5 have consistent APIs for receiving messages. Other<br />

communication APIs, such as the EventSource API and Web Workers, also use MessageEvent to deliver messages.<br />

Building an Application Using the postMessage API<br />

Let’s say that you wanted to build the aforementioned portal application with its cross-origin chat<br />

widget. You can use Cross Document Messaging to communicate between the portal page and the chat<br />

widget, as shown in Figure 5-3.<br />

Figure 5-3. Portal page with cross-origin chat widget iframe

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

Saved successfully!

Ooh no, something went wrong!