Home > Web Front-end > JS Tutorial > An introduction to the usage notes of localStorage in JavaScript

An introduction to the usage notes of localStorage in JavaScript

不言
Release: 2019-04-08 09:38:27
forward
2824 people have browsed it

This article brings you an introduction to the usage of localStorage in JavaScript. 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: JavaScript video tutorial

The above is the detailed content of An introduction to the usage notes of localStorage in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template