Rubens Mariuzzoleiyonglin提出了一个问题:html5 localStorage error with Safari: “QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota.”,或许与您遇到的问题类似。
Apparently this is by design. When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage is available, but trying to call setItem throws an exception.
store.js line 73
"QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota."
What happens is that the window object still exposes localStorage in the global namespace, but when you call setItem, this exception is thrown. Any calls to removeItem are ignored.
I believe the simplest fix (although I haven't tested this cross browser yet) would be to alter the function isLocalStorageNameSupported() to test that you can also set some value.
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浏览器处于私密浏览模式时,localStorage会出现QUOTA_EXCEEDED_ERR错误,尝试设置项会抛出异常,而移除项操作则被忽略。这是因为Safari在这种模式下限制了localStorage的使用。解决办法是在检测localStorage是否可用时,不仅检查其存在,还需尝试设置和移除值来确保功能正常。

被折叠的 条评论
为什么被折叠?



