function() { window.WebStorageLSO = window.WebStorageLSO || { /** * Have we been inited yet? */ isInited: false, /** * The localStorage object, where data will persist until removed */ ls: null, /** * The sessionStorage object, where data will persist until the tab is closed */ ss: null, /** * Checks if localStorage is available and we can use it */ isAvailable: function() { if( !this._hasStorage() ) return false; // now see if it's available - this is because, with some browsers, in // private browsing mode, localStorage is set as available, but we can't // write anything to it. see // http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-safari-quota-exceeded-err-dom-exception-22-an try { this.ss.setItem( 'test', '1' ); this.ss.removeItem( 'test' ); return true; } catch( e ) { return false; } // default return true; }, /** * Adds an item to web storage * @param isForSession Is this for session storage, or persistent storage? * @param key The key of the item to store. Any item with this key will be overwritten * @param val The value of the item to store * @return true if the key/value was added, false otherwise */ add: function( isForSession, key, val ) { if( !this._hasStorage() ) return false; // try and set the item try { if( isForSession ) this.ss.setItem( key, val ); else this.ls.setItem( key, val ); } catch( e ) { return false; } return true; }, /** * Gets an item from web storage * @param isForSession Are we getting an item from session storage, or persistent storage? * @param key The key of the item that we're looking for * @return The String value for key, or null if we don't have it */ get: function( isForSession, key ) { if( !this._hasStorage() ) return null; return ( isForSession ) ? this.ss.getItem( key ) : this.ls.getItem( key ); }, /** * Removes an item from web storage * @param isForSession Are we removing an item from session storage, or persistent storage? * @param key The key of the item that we're removing * @return true if the item was removed, false otherwise */ remove: function( isForSession, key ) { if( !this._hasStorage() ) return false; // remove the item if( isForSession ) this.ss.removeItem( key ); else this.ls.removeItem( key ); return true; }, /** * Removes all our items from web storage. If keyStub is null or empty, then * the web storage is cleared altogether. keyStub is used to prevent accidently * deleted key/value pairs that aren't from this object (e.g. other code, or * another WebStorageLSO) * @param isForSession Are we removing all our items from session storage, or persistent storage? * @param keyStub The key for our keys (i.e. name of the WebStorageLSO) * @return true if all the items were removed, false otherwise */ removeAll: function( isForSession, keyStub ) { if( !this._hasStorage() ) return false; // if the keyStub, is null, or empty, we can just clear the storage if( typeof keyStub === 'undefined' || keyStub === null || keyStub === '' ) { if( isForSession ) this.ss.clear(); else this.ls.clear(); } else { // go through all the objects, and remove anything of ours var s = ( isForSession ) ? this.ss : this.ls; for( var i = s.length - 1; i >= 0; i-- ) { var key = s.key( i ); if( key.indexOf( keyStub ) == 0 ) s.removeItem( key ); } } return true; }, // checks if we have storage, and thus if we can call anything _hasStorage: function() { // init if necessary if( !this.isInited ) { this.isInited = true; // wrap it in a try..catch as if we've set that we can't store local data, // then trying to access it will throw an error try { this.ls = window.localStorage; this.ss = window.sessionStorage; } catch ( e ) { console.error( "Couldn't init WebStorageLSO: " + e ); } // check for undefined if( this.ls === undefined || !this.isAvailable ) { this.ls = null; this.ss = null; } } return ( this.ls !== null && this.ss !== null ); }, } }