1
0
forked from GitHub/gf-core

js/localstorage.js: don't throw an exception if JSON parsing fails

Return the given default value instead.
This commit is contained in:
hallgren
2013-04-23 14:14:38 +00:00
parent 5c19681f2d
commit 322968fb5c

View File

@@ -12,14 +12,18 @@ function supports_html5_storage() {
var fakedLocalStorage = [] // a shared substitute for persistent localStorage
// An interface to localStorage to store JSON data under a unique prefix
// An interface to localStorage, to store JSON data under a unique prefix
function appLocalStorage(appPrefix,privateStorage) {
function parse(s) {
try { return JSON.parse(s) } catch(e) { return null }
}
function methods(storage) {
return {
get: function (name,def) {
var id=appPrefix+name
return storage[id] ? JSON.parse(storage[id]) : def;
return storage[id] && parse(storage[id]) || def;
},
put: function (name,value) {
var id=appPrefix+name;