I didn't find a universal way to get the remaining limit on the browsers I needed but I did find out that when you do reach the limit there is an error message that pops up. This is of-course different in each browser.
To max it out I used this little script:
localStorage.setItem("DATA", "m");
for(i=0 ; i<40 ; i++) {
var data = localStorage.getItem("DATA");
try {
localStorage.setItem("DATA", data + data);
} catch(e) {
console.log("LIMIT REACHED: (" + i + ")");
console.log(e);
}
}
localStorage.removeItem("DATA");
From that I got this information:
Google Chrome
- DOMException:
- code: 22
- message: "QUOTA_EXCEEDED_ERR: DOM Exception 22"
- name: "QUOTA_EXCEEDED_ERR"
Mozilla Firefox
- DOMException:
- code: 1014
- message: "Persistent storage maximum size reached"
- name: "NS_ERROR_DOM_QUOTA_REACHED"
Safari
- Crashed (almost, took about 4 min to recover)
Internet Explorer (community)
- Anyone wanna to try? (I'm on a Mac, no Windows)
My solution
So far my solution is to add an extra call each time the user would save anything. And if the exception is caught then I would tell them that they are running out of storage capacity.
Edit: Delete the added data
I forgot to mention that for this to actually work you would need to delete the DATA
item that was set originally. The change is reflected above by using the removeItem() function.