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 />

The accessibility restrictions on IE user data are similar to the restrictions on cookies. In order to access a<br />

data store, the page on which the script is running must be from the same domain, on the same directory<br />

path, and using the same protocol as the script that saved data to the store. Unlike with cookies, you<br />

cannot change accessibility restrictions on user data to a wider range of consumers. Also unlike cookies,<br />

user data persists across sessions by default and doesn ’ t expire; data needs to be specifically removed<br />

using removeAttribute() in order to free up space.<br />

As with cookies, IE user data is not secure and should not be used to store sensitive<br />

information.<br />

DOM Storage<br />

DOM Storage was first described in the Web Applications 1.0 specification of the Web Hypertext Application<br />

Technical Working Group (WHAT - WG). The initial work from this specification eventually became part of<br />

HTML 5 (see Chapter 21 ), but prior to that point, numerous browsers had implemented DOM Storage. Its<br />

intent is to overcome some of the limitations imposed by cookies when data is needed strictly on the client<br />

side, with no need to continuously send data back to the server. The two primary goals of DOM Storage are:<br />

❑<br />

❑<br />

To provide a way to store session data outside of cookies<br />

To provide a mechanism for storing large amounts of data that persists across sessions<br />

The original DOM Storage specification included definitions for two objects: sessionStorage and<br />

globalStorage . The latter was replaced in HTML 5 by localStorage . Each of these objects has been<br />

implemented by at least one browser and is a global variable, available as a property of window .<br />

DOM Storage has been partially implemented by Firefox 2 and later, and fully<br />

implemented in IE 8 and Safari 3.1. Chrome is expected to implement it before<br />

reaching version 1.0, and Firefox is expected to complete its implementation in<br />

version 3.1.<br />

The Storage Type<br />

The Storage type is designed to hold name - value pairs up to a maximum size (determined by the<br />

browser). An instance of Storage acts like any other object and has the following additional methods:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

clear() — Removes all values; not implemented in Firefox<br />

getItem( name ) — Retrieves the value for the given name<br />

key( index ) — Retrieves the name of the value in the given numeric position<br />

removeItem( name ) — Removes the name - value pair identified by name<br />

setItem( name , value ) — Sets the value for the given name<br />

628

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

Saved successfully!

Ooh no, something went wrong!