本文主要是介绍浏览器本地存储localstorage,sessionStorage,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
浏览器本地存储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
这篇关于浏览器本地存储localstorage,sessionStorage的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!