Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The Web Storage API is surprisingly simple to use. We'll start by covering basic storage and retrieval of values and then move on to the differences between sessionStorage and localStorage. Finally, we'll look at the more advanced aspects of the API, such as event notification when values change.
The storage database for a given domain is accessed directly from the window object. Therefore, determining if a user's browser supports the Web Storage API is as easy as checking for the existence of window.sessionStorage or window.localStorage. Listing 11-1 shows a routine that checks for storage support and displays a message about the browser's support for the Web Storage API. Instead of using this code, you can also use the JavaScript utility library Modernizr, which handles some cases that may result in a false positive.