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.

There are three important points to notice from this storage access statement:<br />

CHAPTER 9 ■ USING THE <strong>HTML</strong>5 WEB STORAGE API<br />

• The object implementing the Web Storage API is attached to the window, so<br />

window.sessionStorage contains the functions you need to call.<br />

• The function we are calling is setItem, which takes a key string and a value string.<br />

Although the formal Web Storage API supports passing in nonstring values,<br />

current browsers are limited in the value types they support.<br />

• This particular call will set into the session storage the string myFirstValue, which<br />

can later be retrieved by the key myFirstKey.<br />

To retrieve the value, the long-hand notation involves making a call to the getItem function. For<br />

example, if we augmented our previous example with the following statement<br />

alert(window.sessionStorage.getItem(‘myFirstKey’));<br />

the browser raises a JavaScript alert displaying the text myFirstValue. As you can see, setting and<br />

retrieving values from the Web Storage API is very straightforward.<br />

However, there is an even simpler way to access the storage objects in your code. You are also able<br />

to use expando-properties to set values in storage. Using this approach, the setItem and getItem calls<br />

can be avoided entirely by simply setting and retrieving values corresponding to the key-value pair<br />

directly on the sessionStorage object. Using this approach, our value set call can be rewritten as follows:<br />

window.sessionStorage.myFirstKey = ‘myFirstValue’;<br />

Similarly, the value retrieval call can be rewritten like so:<br />

alert(window.sessionStorage.myFirstKey);<br />

That’s it for the basics. You now have all the knowledge you need to use session storage in your<br />

application. However, you might be wondering what’s so special about this sessionStorage object. After<br />

all, JavaScript allows you to set and get properties on nearly any object. The difference is in the scope.<br />

What you may not have realized is that our example set and get calls do not need to occur in the same<br />

web page. As long as pages are served from the same origin—the combination of scheme, host, and<br />

port—then values set on sessionStorage can be retrieved from other pages using the same keys. This<br />

also applies to subsequent loads of the same page. As a developer, you are probably used to the idea that<br />

changes made in script will disappear whenever a page is reloaded. That is no longer true for values that<br />

are set in the Web Storage API; they will continue to exist across page loads.<br />

Plugging Data Leaks<br />

How long do the values persist? For objects set into sessionStorage, they will persist as long as the<br />

browser window (or tab) is not closed. As soon as a user closes the window—or browser, for that<br />

matter—the sessionStorage values are cleared out. It is useful to consider a sessionStorage value to be<br />

somewhat like a sticky note reminder. Values put into sessionStorage won’t last long, and you should<br />

not put anything truly valuable into them, as the values are not guaranteed to be around whenever you<br />

are looking for them.<br />

Why, then, would you choose to use the session storage area in your web application? Session<br />

storage is perfect for short-lived processes that would normally be represented in wizards or dialogs. If<br />

you have data to store over the course of a few pages, that you would not be keen to have resurface the<br />

217

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

Saved successfully!

Ooh no, something went wrong!