QuotaExceededError: Dom exception 22: An attempt was made to add something to storage that exceeded the quota

后端 未结 9 1607
夕颜
夕颜 2020-12-04 05:42

Using LocalStorage on iPhone with iOS 7 throws this error. I\'ve been looking around for a resolvant, but considering I\'m not even browsing in private, nothing is relevant.

9条回答
  •  借酒劲吻你
    2020-12-04 06:08

    Here is an expanded solution based on DrewT's answer above that uses cookies if localStorage is not available. It uses Mozilla's docCookies library:

    function localStorageGet( pKey ) {
        if( localStorageSupported() ) {
            return localStorage[pKey];
        } else {
            return docCookies.getItem( 'localstorage.'+pKey );
        }
    }
    
    function localStorageSet( pKey, pValue ) {
        if( localStorageSupported() ) {
            localStorage[pKey] = pValue;
        } else {
            docCookies.setItem( 'localstorage.'+pKey, pValue );
        }
    }
    
    // global to cache value
    var gStorageSupported = undefined;
    function localStorageSupported() {
        var testKey = 'test', storage = window.sessionStorage;
        if( gStorageSupported === undefined ) {
            try {
                storage.setItem(testKey, '1');
                storage.removeItem(testKey);
                gStorageSupported = true;
            } catch (error) {
                gStorageSupported = false;
            }
        }
        return gStorageSupported;
    }
    

    In your source, just use:

    localStorageSet( 'foobar', 'yes' );
    ...
    var foo = localStorageGet( 'foobar' );
    ...
    

提交回复
热议问题