Upstatement / jquery-total-storage

A jQuery plugin to manage local storage and cookies simultaneously in a simple interface
http://upstatement.com/blog/2012/01/jquery-local-storage-done-right-and-easy/
151 stars 66 forks source link

In IE7, objects are not Stringified #9

Open jrrbru opened 11 years ago

jrrbru commented 11 years ago

The function setItem, beginning on line 98 of jquery.total-storage.js, has a bug in it:

setItem: function(key, value){
    if (!supported){
        try {
            $.cookie(key, value);
            return value;
        } catch(e){
            console.log('Local Storage not supported by this browser. Install the cookie plugin on your site to take advantage of the same functionality. You can get it at https://github.com/carhartl/jquery-cookie');
        }
    }
    var saver = JSON.stringify(value);
    ls.setItem(key, saver);
    return this.parseResult(saver);
}

This code fails to stringify value if localStorage is not supported. A fixed version of the function follows:

setItem: function(key, value){
    var saver = JSON.stringify(value);

    if (!supported){
        try {
            $.cookie(key, saver);
        } catch(e){
            if (window.console) { console.log('Local Storage not supported by this browser. Install the cookie plugin on your site to take advantage of the same functionality. You can get it at https://github.com/carhartl/jquery-cookie'); }
        }
    } else {
        ls.setItem(key, saver);
    }

    return this.parseResult(saver);
}

Note that I've also added a check for window.console, as this prevents an error in IE7 ("console is undefined").

dechateau commented 8 years ago

Had the same problem, thanks for your bugfix. Would be nice if there will be a stable version soon even this plugin is kinda "old". .