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.

216<br />

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

Figure 9-1. Checking for browser support<br />

Many browsers do not support sessionStorage for files accessed directly from the file system. Make<br />

sure you serve up the pages from a web server when you run the examples in this chapter. For example,<br />

you can start Python’s simple HTTP server in the code/storage directory as follows:<br />

python -m SimpleHTTPServer 9999<br />

After that, you can access the files at http://localhost:9999/. For example,<br />

http://localhost:9999/browser-test.html.<br />

■ Note As with many APIs, it is possible that a given browser supports only some of the standard. However,<br />

because the Web Storage API is quite small, consistent implementation is already widespread. Consider, also, that<br />

users may choose to turn off Web Storage in their browsers for security reasons even if the browser itself supports<br />

the API.<br />

Setting and Retrieving Values<br />

For now, we’ll focus on the session storage capability as you learn to set and retrieve simple values in a<br />

page. Setting a value can easily be done in a single statement, which we’ll initially write using the longhand<br />

notation:<br />

window.sessionStorage.setItem(‘myFirstKey’, ‘myFirstValue’);

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

Saved successfully!

Ooh no, something went wrong!