localStorage and sessionStorage
The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.
localStorage is similar to sessionStorage, except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed. (localStorage data for a document loaded in a "private browsing" or "incognito" session is cleared when the last "private" tab is closed.)
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 4 | 12 | 3.5 | 4 | 18 | 3.2 | |
| The Storage interface of the Web Storage API provides access to a particular domain's session or local storage. It allows, for example, the addition, modification, or deletion of stored data items. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The clear() method of the Storage interface clears all keys stored in a given Storage object. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The getItem() method of the Storage interface, when passed a key name, will return that key's value, or null if the key does not exist, in the given Storage object. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The key() method of the Storage interface, when passed a number n, returns the name of the nth key in a given Storage object. The order of keys is user-agent defined, so you should not rely on it. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The length read-only property of the Storage interface returns the number of data items stored in a given Storage object. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The removeItem() method of the Storage interface, when passed a key name, will remove that key from the given Storage object if it exists. The Storage interface of the Web Storage API provides access to a particular domain's session or local storage. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The setItem() method of the Storage interface, when passed a key name and value, will add that key to the given Storage object, or update that key's value if it already exists. | 4 | 12 | 3.5 | 4 | 18 | 3.2 |
| The StorageEvent interface is implemented by the Window/storage_event event, which is sent to a window when a storage area the window has access to is changed within the context of another document. | 1 | 12 | 13 | 4 | 18 | 3 |
| The key property of the StorageEvent interface returns the key for the storage item that was changed. | 1 | 12 | 13 | 4 | 18 | 3 |
| The newValue property of the StorageEvent interface returns the new value of the storage item whose value was changed. | 1 | 12 | 13 | 4 | 18 | 3 |
| The oldValue property of the StorageEvent interface returns the original value of the storage item whose value changed. | 1 | 12 | 13 | 4 | 18 | 3 |
| The storageArea property of the StorageEvent interface returns the storage object that was affected. | 3 | 12 | 13 | 4 | 18 | 3 |
| The StorageEvent() constructor creates a new StorageEvent object. | 17 | 17 | 13 | 6 | 18 | 6 |
| The url property of the StorageEvent interface returns the URL of the document whose storage changed. | 6 | 12 | 13 | 5 | 18 | 5 |
| The read-only sessionStorage property accesses a session Storage object for the current origin. sessionStorage is similar to Window.localStorage; the difference is that while localStorage is partitioned by origin only, sessionStorage is partitioned by both origin and browser tabs (top-level browsing contexts). The data in sessionStorage is only kept for the… | 4 | 12 | 2 | 4 | 18 | 3.2 |
| The storage event of the Window interface fires when another document that shares the same storage area (either Window/localStorage or Window/sessionStorage) as the current window updates that storage area. The event is not fired on the window that made the change. | 1 | 15 | 45 | 4 | 18 | 4 |
Syntax
localStorage.setItem("myCat", "Tom"); Use cases
-
Using localStorage and sessionStorage
The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.