Chapter 3. Working with Web Storage
Web Storage, AKA Local Storage
Web storage is the formal name for a technology most of us call Local Storage. Out of all the client-side technologies we’ll discuss in this book, the Web Storage API is probably the quickest to learn and simplest to pick up. It is an API focused on setting and retrieving simple values by keys—so, for example, storing the value
Ray for the key
name. Or storing the value
43 for the key
age. Complex data (like arrays or objects) is not supported, but you can store it by encoding the values into JSON first. (And obviously you’ll need to decode them on retrieval.)
Web storage comes in two flavors: Local Storage and Session Storage. Both use the exact same API, but while Local Storage will persist forever (or until the user clears it), Session Storage will go away as soon as the browser is closed. Since most people use the persistent version, most developers use (and talk about) Local Storage. The official specification for the Web Storage API can be found at http://www.w3.org/TR/webstorage.
Just like cookies (and every other technology covered in this book), web storage is unique to a particular domain. Unlike cookies, there is no magical way to make data stored at www.foo.com also available at app.foo.com. (There are fancy workarounds with iframes, but let’s avoid that for now.) Basically this all means that using a web storage key called
name is completely safe for both foo.com and goo.com—they won’t conflict.