浏览器本地存储localstorage,sessionStorage
文章目录
- 一、localstorage
- localstorage存储
- localstorage读取
- localstorage删除
- localstorage清空
- 二、sessionStorage
- localstorage读取
- localstorage删除
- localstorage清空
- 总结
一、localstorage
即使浏览器关闭,本地回话存储还是会存在
localstorage存储
function saveData(){
localStorage.setItem( 'msg', 'hello!!!')
localStorage.setItem( 'msg2',666)
localStorage.setItem( 'person',JsON.stringify(p))
}
localstorage读取
localStorage.getItem('msg')
localStorage.getItem('msg2')
const result = localStorage.getItem('person')
console.log(JSON.parse(result))
localstorage删除
localStorage.deleteItem('msg')
localstorage清空
localStorage.clear('msg')
二、sessionStorage
浏览器一关闭,则消失
function saveData(){
sessionStorage.setItem( 'msg', 'hello!!!')
sessionStorage.setItem( 'msg2',666)
sessionStorage.setItem( 'person',JsON.stringify(p))
}
localstorage读取
sessionStorage.getItem('msg')
sessionStorage.getItem('msg2')
const result = sessionStorage.getItem('person')
console.log(JSON.parse(result))
localstorage删除
sessionStorage.deleteItem('msg')
localstorage清空
sessionStorage.clear('msg')
总结
1、localstorage,sessionStorage统称为WebStorage
2、存储内容大小一般为5MB左右,不同浏览器可能不一样
3、浏览器通过window.localstorage和window.sessionStorage的属性方法实现本地存储机制
4、如果对应获取不到属性值则返回null