Search code examples
javascriptlocal-storagesession-storage

How to check if HTML5 session storage has enough space for storing new item


I want to store an array of items in html5 session storage. But before storing new items in array I want to see if there is enough space and if it doesn't has enough I want remove first element and add new item. How I can know about available space in session storage?


Solution

  • Try to put the data there and catch possible error:

    try {
        sessionStorage.setItem('key', data);
    } catch (e) {
        if (e.name === 'QuotaExceededError') {
            /* do something else */
        }
    }