localStorage、sessionStorage用法以及区别
设置:
sessionStorage.setItem("key", "value");
localStorage.setItem("site", "js8.in");
var storage = window.localStorage; storage.key1 = "hello"; storage["key2"] = "world";
获取:
var value = sessionStorage.getItem("key");
var site = localStorage.getItem("site");
删除:
sessionStorage.removeItem("key");
localStorage.removeItem("site");
清除:
sessionStorage.clear();
localStorage.clear();
区别:前者是永久存储,后者关闭浏览器时存储数据就会丢失(本地存储只能存储字符串,若为其他类型,需要转为字符串格式)