04.11.2015 Views

javascript

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

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

Chapter 21: Upcoming API s<br />

(continued)<br />

div.dataset.appId = 23456;<br />

div.dataset.set(“myname”, “Michael”);<br />

//is there a “myname” value?<br />

if (div.dataset.has(“myname”)){<br />

alert(“Hello, “ + div.dataset.myname);<br />

}<br />

Because the names of the methods for DOMStringMap have not yet been determined, this code is an<br />

example usage of the anticipated clarifications.<br />

Custom data attributes are useful when nonvisual data needs to be tied to an element for some other<br />

form of processing. This is a common technique to use for link tracking and mashups in order to better<br />

identify parts of a page.<br />

Cross - Document Messaging<br />

One of the challenging areas of web development is how to allow documents from different domains to<br />

communicate with one another. The cross - domain security policy instituted by browsers is considered<br />

necessary to prevent malicious sites from interacting with others. This same restriction, however, has<br />

made it difficult to produce mashups that have data coming from a number of sources. It ’ s quite<br />

common to use < iframe > elements to embed information from another domain, but at this time, the<br />

containing page and the < iframe > content can ’ t communicate in any way.<br />

The HTML 5 cross - document messaging system is designed specifically to enable this communication in<br />

a safe way. Instead of allowing direct access to another document ’ s DOM and associated information,<br />

documents are permitted to send and receive messages containing data.<br />

Cross - document messaging is executed using the postMessage() method on any window object<br />

(including those representing iframes). Unlike other properties and methods of a window object,<br />

postMessage() is always accessible by all other documents, even those from a different domain. This<br />

method accepts two arguments: data to be sent to the document and the target domain for the data. The<br />

second argument is a security feature, ensuring that the data is sent only to documents from appropriate<br />

domains. If the messages should go to any domain, the second argument can be set to an asterisk.<br />

When postMessage() is called on a window object, and the target domain is the same as the window ’ s<br />

domain, the message event fires on the document . The event object created for a message event has the<br />

following properties:<br />

❑<br />

❑<br />

❑<br />

data — The data that was passed in as the first argument to postMessage()<br />

origin — The protocol, domain, and port number of the window that sent the message<br />

source — The window object that sent the message<br />

This information allows the receiver to determine if the message is from a valid source.<br />

676

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

Saved successfully!

Ooh no, something went wrong!