Home  >  Article  >  Web Front-end  >  A brief introduction to the use of localStorage method in js

A brief introduction to the use of localStorage method in js

不言
不言Original
2018-08-22 17:00:0512441browse

This article brings you a brief introduction to the use of the localStorage method in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

localStorage provides several methods:

1. Storage: localStorage.setItem(key, value)

If the key exists, update the value

2. Get: localStorage.getItem(key)

If the key does not exist, return null

3. Delete: localStorage.removeItem(key)

Once deleted, the corresponding key All data will be deleted

4. Clear all: localStorage.clear()

Sometimes it is too troublesome to use removeItem to delete one by one. You can use clear. The consequence of execution is that all localStorage objects will be cleared. Saved data

5. Traverse the total amount of key

.length stored in localStorage, for example: localStorage.length
.key(index) to obtain the key, for example: var key=localStorage .key(index);

6. Store JSON format data

JSON.stringify(data) Convert an object into a JSON format data string and return the converted string
JSON .parse(data) parses the data into an object and returns the parsed object

Note: The data stored in localStorage cannot be shared across browsers. A browser can only read the data of its own browser. Storage space 5M.

Related recommendations:

jQuery localStorage implements timer

How to use localstorage and sessionstorage in Vue

The above is the detailed content of A brief introduction to the use of localStorage method in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn