localStorage和sessionStorage的区别:
localStorage(本地存储) 大(5M)/cookie(4K)
1.大:1280倍
2.localStorage不会被发往服务器
3.方便
localStorage/sessionStorage
localStorage 永久存储
sessionStorage 会话期存储(浏览器关了就没了)
localStorage的用法跟json一模一样
localStorage.a=xxx 设置
localStorage.a 读取
delete localStorage.a 删掉
for in 遍历
sessionStorage用法和localStorage一模一样
1.存一个对象进localStorage
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> let obj={ name: ‘blue‘, age: 18 }; localStorage.a=JSON.stringify(obj); let obj2=JSON.parse(localStorage.a); alert(obj2.name); </script> </head> <body> </body> </html>