1.localStorage是持久存储,sessionStorage是关闭窗口后失效
2.只能存储String类型字符串
3.localStorage的使用是遵循同源策略的,不同的网站直接是不能共用相同的localStorage,意思是在一个网站设置了localStorage的值,到另一个网站是不可以获取到的
常规操作:
//存
localStorage.setItem('obj',JSON.stringify({name:"xiaobai",age:13}));
//取
const obj = JSON.parse(localStorage.getItem("obj"));
//移除
localStorage.removeItem("obj ");
//清空
localStorage.clear();
//遍历键
Object.keys(window.localStorage).forEach(item => console.log(`${item}:${window.localStorage.getItem(item)}`));
//监听
window.addEventListener("storage",(e)=>{
console.log(e);
const key = e.key,oldval = e.oldValue,newval = e.newValue;
});