javascript local storage (introduction)

1.localStorage 和 sessionStorage

The localStorage and sessionStorage attributes allow key/value pair data to be stored in the browser .

(1) Introduction to localstorage

localStorage is used to save the data of the entire website for a long time . The saved data has no expiration time until it is manually deleted.

The localStorage property is read-only.

// 存储(保存数据)
localStorage.setItem("lastname", "Smith");键值对存储
// 检索(读取数据)//获取键所应的值
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
//删除数据
localStorage.removeItem("key");

(2) Introduction to sessionstorage

 If you just want to save the data in the current session, you can use the  sessionStorage  attribute. The data object temporarily saves the data of the same window (or tab), and the data will be deleted after the window or tab is closed.

// 存储
sessionStorage.setItem("lastname", "Smith");
// 检索
document.getElementById("result").innerHTML = sessionStorage.getItem("lastname");
//删除某个键值对
sessionStorage.removeItem("key");

2. Cookie introduction

Cookie does not mean "cookie" in its original meaning, but a simple text file saved in the client computer. This file is associated with a specific  Web  document and saves the information when the client accesses the Web document. , When the client accesses this Web document again, this information is available for the document. Because "Cookie" has the magical feature that can be stored on the client, it can help us realize the function of recording user's personal information, 

 

Guess you like

Origin blog.csdn.net/L_Z_jay/article/details/109205478