Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use sessionStorage and localStorage

Shulou Source: shulou.com Published: 2022-06-02 12:03:44 09月30日 Update

This article will explain in detail how to use sessionStorage and localStorage. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

/ * sessionStorage is used to store data in a session (session) locally, which can only be accessed by pages in the same session and destroyed when the session ends. So sessionStorage is not persistent local storage, just session-level storage. On the other hand, localStorage is used for persistent local storage, and the data will never expire unless it is actively deleted. * / browser local value function my1 () {var name = document.getElementById ("username") .value Var pwd = document.getElementById ("pwd"). Value; / / stores the value in the local browser sessionStorage.setItem ("name", name); sessionStorage.setItem ("pwd", pwd) Alert ("chenggong!"); localStorage.setItem ("name1", name); localStorage.setItem ("pwd1", pwd) } function my2 () {/ / clear the data stored in the browser localStorage.clear () LocalStorage.removeItem ("name1"); sessionStorage.removeItem ("name"); sessionStorage.clear () / / get the values stored in the local browser var name = sessionStorage.getItem ("name"); var pwd = sessionStorage.getItem ("pwd"); alert ("name:" + name+ "--pwd:" + pwd) } this is the end of the article on "how to use sessionStorage and localStorage". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Storage data browser browsing articles more good practical active content only articles knowledge levels pages references help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Docker Redmi Microsoft