jquery-total-storage icon indicating copy to clipboard operation
jquery-total-storage copied to clipboard

In IE7, objects are not Stringified

Open jrrbru opened this issue 11 years ago • 1 comments

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").

jrrbru avatar Jul 18 '13 13:07 jrrbru

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". .

dechateau avatar May 10 '16 14:05 dechateau