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.

136<br />

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

Listing 5-10. Using postMessage in an iframe to handshake with a trusted partner page<br />

var framebustTimer;<br />

var timeout = 3000; // 3 second framebust timeout<br />

if (window !== window.top) {<br />

framebustTimer = setTimeout(<br />

function() {<br />

window.top.location = location;<br />

}, timeout);<br />

}<br />

window.addEventListener(“message”, function(e) {<br />

switch(e.origin) {<br />

case trustedFramer:<br />

clearTimeout(framebustTimer);<br />

break;<br />

}<br />

), true);<br />

Summary<br />

In this chapter, you have seen how <strong>HTML</strong>5 Cross Document Messaging and XMLHttpRequest Level 2<br />

can be used to create compelling applications that can securely communicate cross-origin.<br />

First, we discussed postMessage and the origin security concept—two key elements of <strong>HTML</strong>5<br />

communication—and then we showed you how the postMessage API can be used to communicate<br />

between iframes, tabs, and windows.<br />

Next, we discussed XMLHttpRequest Level 2—an improved version of XMLHttpRequest. We<br />

showed you in which areas XMLHttpRequest has been improved; most importantly in the<br />

readystatechange events area. We then showed you how you can use XMLHttpRequest to make crossorigin<br />

requests and how to use the new progress events.<br />

Finally, we wrapped up the chapter with a few practical examples. In the next chapter, we’ll<br />

demonstrate how <strong>HTML</strong>5 WebSockets enables you to stream real-time data to an application with<br />

incredible simplicity and minimal overhead.

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

Saved successfully!

Ooh no, something went wrong!