牛骨文教育服务平台(让学习变的简单)
博文笔记

ios 中 localstorage有时失效的问题

创建时间:2017-08-04 投稿人: 浏览次数:162

        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;}}

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。