localStorage.setItem(i,i);
}获取数据:localStorage.getItem(key) 获取全部数据:localStorage.valueOf() 示例:
localStorage.getItem(i);
}删除数据:localStorage.removeItem(key) 示例:
localStorage.removeItem(i);
}清空全部数据:localStorage.clear() 获取本地存储数据数量:localStorage.length 获取第 N 个数据的 key 键值:localStorage.key(N)
2. 遍历 key 键值方法
console.log(‘第’+ (i+1) +’条数据的键值为:’ + localStorage.key(i) +’,数据为:’ + localStorage.getItem(localStorage.key(i)));
}
3. 存储大小限制测试及异常处理
3.1 数据存储大小限制测试
不同浏览器对 HTML5 的本地存储大小基本均有限制,一个测试的结果如下:
firefox 22.0 > 5242875 + 5 = 5242880
chrome 28.0 > 2621435 + 5 = 2621440
safari 5.1 > 2621435 + 5 = 2621440
opera 12.15 > 5M (超出则会弹出允许请求更多空间的对话框)
测试代码参考:
function log( msg ) {
console.log(msg);
alert(msg);
}
var limit;
var half = ‘1’; //这里会换成中文再跑一遍
var str = half;
var sstr;
while ( 1 ) {
try {
localStorage.clear();
str += half;
localStorage.setItem( ‘cache’, str );
half = str;
} catch ( ex ) {
break;
}
}
var base = str.length;
var off = base / 2;
var isLeft = 1;
while ( off ) {
if ( isLeft ) {
end = base – (off / 2);
} else {
end = base + (off / 2);
}
sstr = str.slice( 0, end );
localStorage.clear();
try {
localStorage.setItem( ‘cache’, sstr );
limit = sstr.length;
isLeft = 0;
} catch ( e ) {
isLeft = 1;
}
base = end;
off = Math.floor( off / 2 );
}
log( ‘limit: ‘ + limit );
3.2 数据存储异常处理
localStorage.setItem(key,value);
}catch(oException){
if(oException.name == ‘QuotaExceededError’){
console.log(‘超出本地存储限额!’);
//如果历史信息不重要了,可清空后再设置
localStorage.clear();
localStorage.setItem(key,value);
}
}
同事开发webapp(图片应用)遇到一些问题,如兼容uc浏览器,那么webapp开发在业内有没有比较成功的例子? | 同事开发webapp(图片应用)遇到一些问题,如兼容uc浏览器,那么webapp开发在业内有没有比较成功的例子? ...
如何使用vuex结合localstorage动态监听storage变化 | 如何使用vuex结合localstorage动态监听storage变化 ...