Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cope with size limits for Web Storage?

The W3C spec for Web Storage (colloquially known as HTML5 local storage, although this is a misnomer) allows user agents to enforce a size limit, which is recommended at 5MB. According to various sources I've found around the net, a lot of modern browsers use this 5MB limit, except IE which gives 10MB.

First, I want to know, does the limit apply to the combined total of localStorage and sessionStorage, or to them individually?

Now, how do we actually cope with these limits? Is there any way to find out what the limit is for a particular browser? Is there any website which maintains statistics on the different browsers? Is an exception thrown when the browser goes over the limit? Is there a foolproof way to test for this exception "class" in different browsers?

like image 552
Adam Burley Avatar asked Nov 04 '22 11:11

Adam Burley


1 Answers

AFAIK, there is no method in localStorage object to determine its size. However, trying to write an amount of data that exceeds storage limit will result in an exception you can catch.

Take a look at http://arty.name/localstorage.html

Basically, you can wrap the writing function in a try...catch block like this:

function write(value, name) {
    try {
        localStorage.field = value;
        return true;
    } catch (e) {
        return false;
    }
}
like image 138
unclenorton Avatar answered Nov 09 '22 09:11

unclenorton