javascript - How does one implement a local storage array? -


i tried made sense:

in module below localstorage.foo works localstorage.session_array['privacy'] returns undefined.

this prototype code modern broswsers.

var isession = ( function ()  {      localstorage.session_array =      {         privacy:            0     };      localstorage.foo = 1;       var sessioni = function ( )      {     };      sessioni.prototype.get = function( type )      {         return localstorage.session_array[ type ];     };      sessioni.prototype.set = function( type, value )      {         localstorage.session_array[ type ] = value;         alert( '|' + localstorage.foo ); // returns 1         alert( '|' + localstorage.session_array[ 'privacy' ] ); // returns undefined     };      return sessioni;  } ) (); 

in mean while i'm going implement using non array properties.

since localstorage isnt supported browsers, might want @ store.js instead. has support localstorage when available , if not uses other storage mechanisms (globalstorage , userdata). best part can store json encoded data well.

// store 'marcus' @ 'username' store.set('username', 'marcus')  // 'username' store.get('username')  // remove 'username' store.remove('username')  // clear keys store.clear()  // store object literal - store.js uses json.stringify under hood store.set('user', { name: 'marcus', likes: 'javascript' })  // stored object - store.js uses json.parse under hood var user = store.get('user') alert(user.name + ' likes ' + user.likes)  store.set('user', { name: 'marcus', likes: 'javascript' }) alert("hi name " + store.get('user').name + "!")  store.set('tags', ['javascript', 'localstorage', 'store.js']) alert("we've got " + store.get('tags').length + " tags here") 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -