Name

Storage — client-side storage of name/value pairs

Synopsis

The localStorage and sessionStorage properties of Window are both Session objects that represent persistent client-side associative arrays that map string keys to values. In theory, a Session object can store any value that can be cloned with the structured clone algorithm (see Structured Clones). At the time of this writing, however, browsers only support string values.

The methods of a Storage object allow you to add new key/value pairs, remove key/value pairs, and query the value associated with a specified key. You don’t have to call these methods explicitly, however: you can use array indexing and the delete operator in their place, and treat localStorage and sessionStorage as if they were ordinary JavaScript objects.

If you change the contents of a Storage object, any other Windows that have access to the same storage area (because they’re displaying a document from the same origin) will be notified of the change with a StorageEvent.

Properties

readonly unsigned long length

The number of key/value pairs stored.

Methods

void clear()

Removes all stored key/value pairs.

any getItem(string key)

Returns the value associated with key. (In implementations current at the time of this writing, the return value will always be a string.) This method is invoked implicitly when you index the Storage object to retrieve a property named key.

string key(unsigned long n)

Returns the nth key in this Storage object or null if n is greater than ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.