1. Introduction
localStorage can directly store the data requested for the first time locally, which is equivalent to a 5M -sized database for front-end pages
——Note : The localStorage attribute is only supported in IE versions above IE8 . localStorage belongs to permanent storage . If there is a lot of storage content, it will consume memory space and cause the page to become stuck.
2. The specific usage is as follows:
1. localStorage - data storage with no time limit
var arr = [1,2,3]; localStorage.setItem("temp",arr); //Store parameters: 1. The value to be called 2. The data to be stored console.log(localStorage.getItem("temp"));//输出 2. Empty localStorage localStorage.clear(); // 3. Delete key-value pairs localStorage.removeItem("arr");
Note: The stored data can only be stored in the form of strings .
3. Provide the method of transferring JOSN data:
//JSON object to JSON string
var obj = {"a": 1,"b": 2}; obj = JSON.stringify(obj); //Convert to JSON string localStorage.setItem("temp2", obj);
//JSON string to JSON object obj=JSON.parse(localStorage.getItem("temp2"));