Web Storage API
- Previous Page Web History API
- Next Page Web Worker API
The Web Storage API is a simple syntax for storing and retrieving data in the browser. It is very easy to use:
Instance
localStorage.setItem("name", "Bill Gates"); localStorage.getItem("name");
All browsers support the Web Storage API:
Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | IE/Edge | Firefox | Safari | Opera |
Supports | Supports | Supports | Supports | Supports |
localStorage object
The localStorage object provides access to the local storage of a specific website. It allows you to store, read, add, modify, and delete data items in the domain.
The stored data does not have an expiration date and will not be deleted when the browser closes.
These data will be available for several days, weeks, and years.
setItem() method
The localStorage.setItem() method stores data items in storage.
It accepts a name and a value as parameters:
Instance
localStorage.setItem("name", "Bill Gates");
getItem() method
The localStorage.getItem() method retrieves data items from the storage (storage).
It accepts a name as a parameter:
Instance
localStorage.getItem("name");
sessionStorage object
The sessionStorage object is the same as the localStorage object.
The difference is that the sessionStorage object stores session data.
Data will be deleted when the browser closes.
Instance
sessionStorage.getItem("name");
setItem() method
The sessionStorage.setItem() method stores data items in the storage (storage).
It accepts a name and a value as parameters:
Instance
sessionStorage.setItem("name", "Bill Gates");
getItem() method
The sessionStorage.getItem() method retrieves data items from the storage (storage).
It accepts a name as a parameter:
Instance
sessionStorage.getItem("name");
Storage object properties and methods
Property/Method | Description |
---|---|
key(n) | Return the name of the nth key in the storage. |
length | Return the number of data items stored in the Storage object. |
getItem(keyname) | Return the value specified by the key name. |
setItem(keyname, value) | Add the key to storage, or update the value of the key if it already exists. |
removeItem(keyname) | Remove the key from storage. |
clear() | Clear all keys. |
Pages related to Web Storage API
Attribute | Description |
---|---|
window.localStorage | Allow saving key/value pairs in Web browsers. Store data without an expiration date. |
window.sessionStorage | Allow saving key/value pairs in Web browsers. Store data for a session. |
- Previous Page Web History API
- Next Page Web Worker API