草庐IT

javascript - Safari : "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." 的 html5 localStorage 错误

coder 2023-07-03 原文

我的 webapp 在 ios safari 隐私浏览中有 javascript 错误:

JavaScript:error

undefined

QUOTA_EXCEEDED_ERR:DOM Exception 22:An attempt was made to add something to storage...

我的代码:

localStorage.setItem('test',1)

最佳答案

显然这是设计使然。当 Safari(OS X 或 iOS)处于隐私浏览模式时,看起来好像 localStorage 可用,但尝试调用 setItem 会引发异常。

store.js line 73
"QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota."

发生的情况是,窗口对象仍然在全局命名空间中公开localStorage,但是当您调用setItem 时,会抛出此异常。对 removeItem 的任何调用都将被忽略。

我相信最简单的修复(虽然我还没有测试过这个跨浏览器)是改变函数 isLocalStorageNameSupported() 来测试你是否也可以设置一些值。

https://github.com/marcuswestin/store.js/issues/42

function isLocalStorageNameSupported() 
{
    var testKey = 'test', storage = window.sessionStorage;
    try 
    {
        storage.setItem(testKey, '1');
        storage.removeItem(testKey);
        return localStorageName in win && win[localStorageName];
    } 
    catch (error) 
    {
        return false;
    }
}

关于javascript - Safari : "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." 的 html5 localStorage 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14555347/

有关javascript - Safari : "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." 的 html5 localStorage 错误的更多相关文章

随机推荐