I need to save a large amount of data through web browser localStorage intercepting the instance when it fills. I searched a lot and the only way I could find is calling a boolean method which try to makes new entries. I'd prefer to avoid this method.

I would like to know if a localStorage JavaScript function to know if it's full exists.

有帮助吗?

解决方案

No such method exists. The only way to test the storage capacity, is by attempting to add to storage, and catching the exception. See the spec for available methods of localStorage.

其他提示

The way normally used to prevent such interception is to add a check for the QUOTA_EXCEEDED_ERR like

try {
    localStorage.setItem('item', val);
} catch (e) {
    if (e == QUOTA_EXCEEDED_ERR) {
        alert('Quota exceeded!');
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top