【Browser】JavaScript storage object, sessionStorage (session storage) and localStorage (local storage)

JavaScript storage object


  The Web Storage API provides two storage objects sessionStorage (session storage) and localStorage (local storage) to add, delete, modify, and query web page data.

  • localStorage is used to save the data of the entire website for a long time, and the saved data has no expiration time until it is manually removed. The localStorage property is read-only.
  • sessionStorage is used to temporarily save the data of the same window (or tab), which will be deleted after closing the window or tab.

storage object method

parameter describe
key(n) Returns the name of the nth key in the storage object
getItem(keyname) Returns the value for the specified key
setItem(keyname, value) Add key and value, if the corresponding value exists, update the value corresponding to the key.
removeItem(keyname) remove key
clear() Clear all keys in the storage object

1.localStorage

Syntax: window.localStorage
Save Data Syntax: localStorage.setItem("key", "value");
Read Data Syntax: var lastname = localStorage.getItem("key");
Delete Data Syntax:localStorage.removeItem("key");

2.sessionStorage

Syntax: window.sessionStorage
save data Syntax: sessionStorage.setItem("key", "value");
read data Syntax: var lastname = sessionStorage.getItem("key");
delete data for specified key Syntax:: sessionStorage.removeItem("key");
delete all data:sessionStorage.clear();

Guess you like

Origin blog.csdn.net/m0_46533551/article/details/129444812