HTML学习笔记之三(localstorage的使用)

时间:2022-08-26 08:26:33
localstorage的使用

1.获取对象

var localstroage = window.localStorage;

2.存储值

localstroage.setItem('openid','ksjhfkjajkdfjkszdjfksad');
localstroage.setItem('uid','10001');

3.获取值

var openid = localstroage.getItem('openid');

4.删除某个值

localstroage.removeItem('openid');


5.删除所有值

localstroage.clear();

6.遍历所有键值

localStorage.uid = 1;
localStorage.sex='nan';
localStorage.age = 23;
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
console.log( localStorage.key( i ) +':' + localStorage.getItem(localStorage.key( i )) );
}

注意:1.  如果不主动删除 localstorage  是不会自动清空的 不会像cookie一样清除

            2.  localstorage 几乎支持大部分浏览器 不用考虑IE8 的问题 大小为 5M