ios中 由子页面setItem值,history.back返回主页面,有时会get不到item值,查询外文资料知道 localstory值仍然是存在的,但是setItem会报异常 quotaExceededError,所以在返回主页面后getItem和removeItem会被忽略。
针对以上问题 我们可以用store.js来解决。store.js 是一个兼容所有浏览器的 LocalStorage 包装器,不需要借助 Cookie 或者 Flash。store.js 会根据浏览器自动选择使用 localStorage、globalStorage 或者 userData 来实现本地存储功能。
使用方法与localstory一致
store.set('username', 'marcus')
store.get('username')
store.remove('username')
store.clear()
顺便写出判断是否支持localstory方法
function isLocalStorageSupported() {
var testKey = 'test',
storage = window.sessionStorage;
try {storage.setItem(testKey, 'testValue');
storage.removeItem(testKey);
return true;}
catch (error)
{return false;}}