admin管理员组

文章数量:1279177

I'm using html5's localStorage API.

I want to catch the QUOTA_EXCEEDED_ERR so I can show a message to the user like "Memory is full. Cannot save. Maybe delete a few items?"

The code that I will use is like

function save() {
    try {
        localStorage.setItem(key, name);
    } catch (e) {
        if (e.name === 'QUOTA_EXCEEDED_ERR') {
            alert("Memory is full. Cannot save. Maybe delete a few items?");
        } else {
            alert("Something went wrong? Try again later?")
        }
    }
}

I want to ask you, is this ok? Will this work fine to all browsers? Will work fine or break, anyways?

Of course I am testing it too, but I thought I should ask anyways, because maybe I am missing something.

I'm using html5's localStorage API.

I want to catch the QUOTA_EXCEEDED_ERR so I can show a message to the user like "Memory is full. Cannot save. Maybe delete a few items?"

The code that I will use is like

function save() {
    try {
        localStorage.setItem(key, name);
    } catch (e) {
        if (e.name === 'QUOTA_EXCEEDED_ERR') {
            alert("Memory is full. Cannot save. Maybe delete a few items?");
        } else {
            alert("Something went wrong? Try again later?")
        }
    }
}

I want to ask you, is this ok? Will this work fine to all browsers? Will work fine or break, anyways?

Of course I am testing it too, but I thought I should ask anyways, because maybe I am missing something.

Share Improve this question edited Mar 14, 2014 at 18:23 code-jaff 9,3304 gold badges37 silver badges56 bronze badges asked Oct 24, 2013 at 19:33 slevinslevin 3,88621 gold badges76 silver badges140 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

e.name for a quota exceeded error won't always necessarily be "QUOTA_EXCEEDED_ERR" in every browser.

It may be "NS_ERROR_DOM_QUOTA_REACHED" in certain versions of FF http://chrisberkhout./blog/localstorage-errors/

May be "QuotaExceededError" or "W3CException_DOM_QUOTA_EXCEEDED_ERR" depending on IE browser version/mode http://msdn.microsoft./en-us/library/ie/cc197050(v=vs.85).aspx

However, you are still catching the error. So worst case scenario is the user will get the "Something went wrong?" alert vs. the "Memory is full" alert.

You'd better use 'Disk storage' instead of 'Memory', because it is more clear. 'Memory' is more often stands for RAM, not persistent storage

本文标签: javascriptcatch QUOTAEXCEEDEDERR on localStorageStack Overflow