HTML5 Web Storage
HTML5 web storage, a better local storage method than cookies.
What is HTML5 Web Storage?
Using HTML5, you can store users' browsing data locally.
In the past, local storage used cookies. But web storage is more secure and faster. This data is not saved on the server, but is only used for data requested by the user's website. It can also store large amounts of data without affecting website performance.
Data is stored in key/value pairs, and a web page's data can only be accessed and used by that web page.
Browser Support
![]()
![]()
![]()
![]()
![]()
Internet Explorer 8+, Firefox, Opera, Chrome, and Safari support Web Storage.
Note:Internet Explorer 7 and earlier IE versions do not support web storage.
localStorage and sessionStorage
The two objects for storing data on the client side are:
- localStorage - used to save data for the entire website for a long time. The saved data has no expiration time until manually removed.
- sessionStorage - used to temporarily save data for the same window (or tab). This data will be deleted after the window or tab is closed.
Before using web storage, you should check whether the browser supports localStorage and sessionStorage:
localStorage Object
Data stored by the localStorage object has no time limit. It is still available the next day, the next week, or the next year.
Example
Try it Yourself »
Example explanation:
- Usekey="sitename"andvalue="Example"to create a localStorage key/value pair.
- Retrieve the value with key "sitename" and insert the data intoid="result"the element.
The above example can also be written as:
Remove "sitename" from localStorage:
Whether it is localStorage or sessionStorage, the available APIs are the same. The commonly used ones are as follows (taking localStorage as an example):
- Save data: localStorage.setItem(key,value);
- Read data: localStorage.getItem(key);
- Delete a single data: localStorage.removeItem(key);
- Delete all data: localStorage.clear();
- Get the key at a certain index: localStorage.key(index);
Tip:Key/value pairs are usually stored as strings, and you can convert the format as needed.
The following example shows the number of times a user clicks a button.
Convert the string values in the code to numeric types:
Example
Try it Yourself »
sessionStorage Object
sessionStorage stores data for a session. When the user closes the browser window, the data will be deleted.
How to create and access a sessionStorage:
Example
Try it Yourself »
Use Web Storage to develop a simple website list program
The website list program implements the following functions:
- You can enter the website name and URL, and store the website name as the key in localStorage;
- Look up the URL based on the website name;
- List all currently saved websites;
The following code is used to save and find data:
save() and find() methods
The complete example demonstration is as follows:
Example
Try it Yourself »
Implementation effect screenshot:

The above example only demonstrates simple localStorage storage and retrieval; in most cases, the data we store is more complex.
Next, we will useJSON.stringifyto store object data,JSON.stringifywhich can convert an object to a string.
After that, we useJSON.parsemethod to convert the string to a JSON object:
JavaScript implementation code:
save() and find() methods
The complete example is as follows:
Example
Try it Yourself »
In the example, loadAll outputs all stored data. You need to ensure that all data stored in localStorage is in JSON format, otherwise JSON.parse(str) will throw an error.
Output result demonstration:
