0

I tried what made sense:

In the module below localStorage.foo works but localStorage.session_array['privacy'] returns undefined.

This is prototype code for 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 the mean while I'm just going to implement this using non array properties.

2 Answers 2

3

localStorage only saves data as strings

Sign up to request clarification or add additional context in comments.

1 Comment

I can't say, but sessionStorage has less persistance. As the name implies the data is only available for the session
1

Since localstorage isnt supported by all browsers, you might want to look at store.js instead. It has support for localstorage when available and if not uses some other storage mechanisms (globalStorage and UserData). The best part is you can also store JSON encoded data as well.

// Store 'marcus' at 'username'
store.set('username', 'marcus')

// Get 'username'
store.get('username')

// Remove 'username'
store.remove('username')

// Clear all keys
store.clear()

// Store an object literal - store.js uses JSON.stringify under the hood
store.set('user', { name: 'marcus', likes: 'javascript' })

// Get the stored object - store.js uses JSON.parse under the hood
var user = store.get('user')
alert(user.name + ' likes ' + user.likes)

store.set('user', { name: 'marcus', likes: 'javascript' })
alert("Hi my name is " + store.get('user').name + "!")

store.set('tags', ['javascript', 'localStorage', 'store.js'])
alert("We've got " + store.get('tags').length + " tags here")

2 Comments

Can you elaborate on what you mean by global storage and user data...?
global storage and user data are different types of local storage mechanisms that are available on different browsers. store.js will use the localstorage api if the browser supports it, otherwise on older browsers it will store in globalstorage or userdata (filesystem), however the javascript code to access the data will be the exact same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.