js local storage: localStorage

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"));

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324573662&siteId=291194637