Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch QUOTA_EXCEEDED_ERR on localStorage

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.

like image 832
slevin Avatar asked Oct 24 '13 19:10

slevin


1 Answers

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.com/blog/localstorage-errors/

May be "QuotaExceededError" or "W3CException_DOM_QUOTA_EXCEEDED_ERR" depending on IE browser version/mode http://msdn.microsoft.com/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.

like image 176
SteamDev Avatar answered Sep 23 '22 18:09

SteamDev