js cookie和session区别(js中本地存储介绍)
js cookie和session区别(js中本地存储介绍)function myClick1(){ var username=document.getElementById('username').value; var pwd=document.getElementById('password').value; // sessionStorage.setItem("key1" username); // sessionStorage.setItem("key2" pwd); localStorage.setItem("key1" username); localStorage.setItem("key2" pwd); alert("存储成功"); localStorage.removeItem("key1"); } function myClick2(){ var username=document.getElementById
<input type="text" id="username">
<input type="password" id="password">
<input type="button" onclick="myClick1()" value="存储" />
<button onclick="myClick2()">获取</button>
function myClick1(){ var username=document.getElementById('username').value; var pwd=document.getElementById('password').value; // sessionStorage.setItem("key1" username); // sessionStorage.setItem("key2" pwd); localStorage.setItem("key1" username); localStorage.setItem("key2" pwd); alert("存储成功"); localStorage.removeItem("key1"); } function myClick2(){ var username=document.getElementById('username').value; var pwd=document.getElementById('password').value; // sessionStorage.getItem("key1"); //生命周期很短,浏览器关闭就被销毁了,即使不关闭,时间也不长 // sessionStorage.getItem("key2"); var a=localStorage.getItem("key1");//浏览器关闭不影响,存储在本地,如果不手动清除cookie,会一直存在 var b=localStorage.getItem("key2"); alert(a "\n" b); //换行 //sessionStorage.removeItem("key1"); //localStorage.removeItem("key1");//每次清除一个 //sessionStorage.clear(); //localStorage.clear();//清除全部 }