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.

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

■ Note You must restart your browser after modifying the host file to ensure the DNS entries take effect.<br />

2. Install Python, which includes the lightweight SimpleHTTPServer web server, if<br />

you did not do so for the previous example.<br />

3. Navigate to the directory that contains the example file<br />

(crossOrignUpload.html) and the Python CORS server script (CORSServer.py).<br />

4. Start Python in this directory as follows:<br />

python CORSServer.py 9999<br />

5. Open a browser and navigate to<br />

http://portal.example.com:9999/crossOriginUpload.html. You should now<br />

see the page shown in Figure 5.5.<br />

Practical Extras<br />

Sometimes there are techniques that don’t fit into our regular examples, but that nonetheless apply to<br />

many types of <strong>HTML</strong>5 applications. We present to you some short, but common, practical extras here.<br />

Structured Data<br />

Early versions of postMessage only supported strings. Later revisions allowed other types of data<br />

including JavaScript objects, canvas imageData, and files. Support for different object types will vary by<br />

browser as the specification develops.<br />

In some browsers, the limitations on JavaScript objects that can be sent with postMessage are the<br />

same as those for JSON data. In particular, data structures with cycles may not be allowed. An example<br />

of this is a list containing itself.<br />

Framebusting<br />

Framebusting is a technique for ensuring that your content is not loaded in an iframe. An application<br />

can detect that its window is not the outermost window (window.top) and subsequently break out of its<br />

containing frame, as shown in the following example.<br />

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

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

}<br />

However, there may be certain partner pages that you want to allow to frame your content. One<br />

solution is to use postMessage to handshake between cooperating iframes and containing pages, as<br />

shown in the Listing 5-11.<br />

135

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

Saved successfully!

Ooh no, something went wrong!