localstorage的使用
1.获取对象
var localstroage = window.localStorage;
2.存储值
localstroage.setItem('openid','hello');
localstroage.setItem('uid','10001');
3.获取值
varopenid = localstroage.getItem('openid');
4.删除某个值
localstroage.removeItem('openid');
5.删除所有值
localstroage.clear();
6.遍历所有键值
localStorage.uid = 1;
localStorage.sex='nan';
localStorage.age = 23;
for(vari = 0, len = localStorage.length; i < len; ++i ) {
console.log( localStorage.key( i ) +':'+ localStorage.getItem(localStorage.key( i )) );
}
注意:
-
如果不主动删除 localstorage 是不会自动清空的 不会像cookie一样清除
-
localstorage 几乎支持大部分浏览器 不用考虑IE8 的问题 大小为 5M
网友评论