How to get quota of localStorage in HTML5

By | June 8, 2012

It is sometimes important to detect the actual quota of storage that the current client browser supports before actually storing data into it. To prevent  the exception QUOTA_EXCEEDED_ERR to happen in the browser, as a developer you will always need to query what are the memory that can be stored without exceeding the quota given by the client to the application.

Let us take a look how to query local storage :

webkitStorageInfo.queryUsageAndQuota( webkitStorageInfo.TEMPORARY,  showData, showerror);
webkitStorageInfo.queryUsageAndQuota( webkitStorageInfo.PERSISTENT,  showData, showerror);


The above two lines of code will queryUsageAndQuota of the browser. If the data cannot be retrieved, it will call the error callback, otherwise it will call showData.

 

function showData (used, remaining) {
/// the used and remaining bytes will be received inside this method automatically.
}

As far as I know, Mozilla Firefox, Chrome, Opera supports 5MB of storage while Internet Exporer supports 10MB of local storage, but this data might change.

 

By the way, you should also note that webkitStorageInfo API is only available in Chrome for time being, but when specs are ready those will be available in other browsers too in near future.

Thanks for reading