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 19: Client - Side Storage<br />

Limits and Restrictions<br />

As with other client - side data storage solutions, DOM Storage also has limitations. These limitations are<br />

browser - specific. Generally speaking, the size limit for client - side data is set on a per - domain basis, so<br />

each domain has a fixed amount of space in which to store its data. Analyzing the domain of the page<br />

that is storing the data enforces this restriction, rather than the domain specified as part of<br />

globalStorage .<br />

In Firefox 2, there was no limit to the amount of data that could be saved; Firefox 3 introduced a 5MB<br />

per - domain limit. IE 8 introduced its DOM Storage support with a limit of 10MB per domain.<br />

Summary<br />

The ability to store data on the client is an increasingly important capability for web applications. This<br />

chapter covered the following aspects of client - side storage:<br />

❑<br />

❑<br />

❑<br />

Traditionally, such storage was limited to using cookies, small pieces of information that could<br />

be set from the client or server and transmitted along with every request.<br />

JavaScript provides access to cookies through document.cookie .<br />

The limitations placed on cookies make them okay for storing small amounts of data, but<br />

inefficient for storing large amounts.<br />

IE provides a behavior called user data that can be applied to an element on the page as follows:<br />

❑<br />

❑<br />

Once applied, the element can load data from a named data store and make the information<br />

accessible via the getAttribute() , setAttribute() , and removeAttribute() methods.<br />

The data must be explicitly saved to a named data store using the save() method for it to<br />

persist between sessions.<br />

DOM Storage was first defined in the Web Applications 1.0 specification and later was absorbed into the<br />

HTML 5 specification as described here :<br />

❑<br />

❑<br />

❑<br />

DOM Storage defines two objects to save data: sessionStorage and localStorage . The former<br />

is used strictly to save data within a browser session, because the data is removed once the<br />

browser is closed. The latter is used to persist data across sessions and based on cross -domain<br />

security policies.<br />

There is a third object, globalStorage , which has been implemented by Firefox as it was<br />

defined in earlier versions of HTML 5.<br />

The localStorage object replaced globalStorage in later versions, so the functionality is<br />

very similar.<br />

With all of these options available, it ’ s possible to store a significant amount of data on the client<br />

machine using JavaScript. You should use care not to store sensitive information, because the data cache<br />

isn ’ t encrypted.<br />

634

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

Saved successfully!

Ooh no, something went wrong!