local storage and session storage

Local storage (localstorge) is a new specification of html5. It is also widely used in the project. It can achieve persistent storage of data (only below 5M), which means that users do not actively clean up or the website does not Active cleaning, it will be kept permanently in your electronic device, using this feature, we use it to persistently save a specific state. For example, some information in the user's personal center, but its biggest disadvantage is that it is stored locally, which is easy to be stolen by illegal websites.

Session, its characteristic is that a user starts a session when he opens the webpage, and closes it when the user closes the webpage. Similarly, it can also store data. The usage of session storage (sessionstorge) is very similar to that of local storage. The difference is that the session storage is destroyed after the user closes the website, and the user does not need to actively clean it up. We use it to temporarily save a specific state, such as storing authentication information on the user login page, the style of the navigation bar, and so on.

Here take localstore as an example, (local storage is used more, session storage is used less)

localstore.setItem("key","value") is stored in the local storage, and then get the corresponding value through the key value through the getItem("key") method, removeItem("key") This method can remove specific data . clear() clears local storage

Local storage can be seen on the console, for example, some data locally stored in my computer by csdn

 session storage

 So what happens when I exit csdn?

 Session storage has been cleared. The local storage will also be cleared when I close the website to store information from other websites

Still take localstorge as an example, he can store arrays as strings, we use JSON.stringify() to store arrays in local storage, such as

let numArr=[1,"abc",true]
localstorge.setItem("num",JSON.stringify(numArr))
console.log(localstorge)

Take out this array using the JSON.parse() method, such as

let takeArr=JSON.parse(localstorge.getItem("num"))
console.log(takeArr)

Guess you like

Origin blog.csdn.net/qq_45662523/article/details/126534091