最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: The quota has been exceeded. on Safari IOS 10 - Stack Overflow

programmeradmin0浏览0评论

I'm getting this error on my iPhone's safari, when doing localStorage.setItem('user',some string here):

Error: The quota has been exceeded.
setItem@[native code]

It is not private mode! What other circumstances can make localStorage not work?

I'm getting this error on my iPhone's safari, when doing localStorage.setItem('user',some string here):

Error: The quota has been exceeded.
setItem@[native code]

It is not private mode! What other circumstances can make localStorage not work?

Share Improve this question edited Aug 29, 2019 at 2:52 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Nov 29, 2016 at 18:47 ice_vitaice_vita 791 gold badge2 silver badges7 bronze badges 1
  • Possible duplicate of html5 localStorage error with Safari: "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." – Alex W Commented Apr 28, 2017 at 20:22
Add a ment  | 

2 Answers 2

Reset to default 2

I created this class to help get around private browsing. However, storage will be blown away when you refresh the browser.

const data = {};
let hasLocalStorage = false;

if (localStorage) {
  try {
    const x = 'storageTest';
    localStorage.setItem(x, x);
    localStorage.removeItem(x);
    hasLocalStorage = true;
  } catch (e) {
    hasLocalStorage = false;
  }
}

class StorageUtilities {
  setItem(key, value) {
    if (hasLocalStorage) {
      localStorage.setItem(key, value);
    } else {
      data[key] = value;
    }
  }

  getItem(key) {
    if (hasLocalStorage) {
      return localStorage.getItem(key);
    }
    return data[key];
  }

  removeItem(key) {
    if (hasLocalStorage) {
      localStorage.removeItem(key);
    } else {
      data[key] = null;
    }
  }
}

const storageUtilities = new StorageUtilities();

export default storageUtilities;

Actually it was Private mode. Looks like it is enabled by default on new iphones.

发布评论

评论列表(0)

  1. 暂无评论