Categories
JavaScript Answers

How to fix HTML5 localStorage error with Safari: “QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota.” with JavaScript?

Spread the love

To fix HTML5 localStorage error with Safari: "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." with JavaScript, we check if the entry with the key can be saved.

For instance, we write

const isLocalStorageNameSupported = (testKey) => {
  const storage = window.localStorage;
  try {
    storage.setItem(testKey, "1");
    storage.removeItem(testKey);
    return true;
  } catch (error) {
    return false;
  }
};

to define the isLocalStorageNameSupported function.

In it, we get the testKey string and call setItem with it to see if we can save the entry with the key without error.

We call removeItem to remove the test entry.

If there’s an error we return false.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *