Skip to content

In IE7, objects are not Stringified #9

@jrrbru

Description

@jrrbru

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions