The difference between JS local storage and session storage

1, localStorage local storage

The localStorage life cycle is permanent, which means that unless the user clears the localStorage information on the UI provided by the browser, the information will always exist. The storage data size is generally 5MB, and it is only stored in the client (ie browser), and does not participate in the communication with the server .

2, sessionStorage session storage

sessionStorage is only valid in the current session and will be cleared after closing the page or browser. The storage data size is generally 5MB, and it is only stored in the client (ie browser), and does not participate in the communication with the server. The source interface can be accepted, and it can also be encapsulated again to have better support for Object and Array .

3. Use the same API when using

 localStorage.setItem("key","value");//以“key”为名称存储一个值“value”

 localStorage.getItem("key");//获取名称为“key”的值

 localStorage.removeItem("key");//删除名称为“key”的信息。

 localStorage.clear();​//清空localStorage中所有信息

Different browsers cannot share the information in localStorage or sessionStorage. Different pages of the same browser can share the same localStorage (pages belong to the same domain name and port), but different pages or tabs cannot share sessionStorage information. It should be noted here that pages and tabs only refer to top-level windows. If a tab contains multiple iframe tags and they belong to the same page, then sessionStorage can be shared between them.

4. Advantages and disadvantages

Disadvantages: not compatible with browsers below ie9.
Advantages: perfect interface

Guess you like

Origin blog.csdn.net/Serena_tz/article/details/113933660