談談這兩個屬性sessionStrong和localStorage是Html5新增點屬性,用來記錄一些數據在瀏覽器。
兩者的區別sessionStrong存儲的數據是暫時的,瀏覽器關掉后,存儲下來的數據就會被清除。
localStorage存儲的數據是長期的,瀏覽器關掉后,存儲下來的數據還是會保存在瀏覽器中。
具體存儲下來的數據可以在瀏覽器的resources中找到。
有了這兩個屬性我們可以做很多的事情,靈活運用,不太需要依賴cookie。
你一定要熱愛你的工作,Web,只有自己喜歡的事情。才會有快樂,才會進步。
技術段:
把數據存儲到瀏覽器:
localStorage.setItem("heName01", "周傑倫");
sessionStorage.setItem("heName02", "林俊傑");
把存儲到數據拿出來用:
var heName01 = localStorage.getItem("heName");
var heName02 = sessionStorage.getItem("heName");
console.log(heName01);//周傑倫
console.log(heName02);//林俊傑
把存儲到數據在瀏覽器中清除:
localStorage.removeItem("heName01");
sessionStorage.removeItem("heName02");
