From 322968fb5ccd399a4c1728655a2222c6c4d849a6 Mon Sep 17 00:00:00 2001 From: hallgren Date: Tue, 23 Apr 2013 14:14:38 +0000 Subject: [PATCH] js/localstorage.js: don't throw an exception if JSON parsing fails Return the given default value instead. --- src/www/js/localstorage.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/www/js/localstorage.js b/src/www/js/localstorage.js index 8af2906a4..3c6a0fa12 100644 --- a/src/www/js/localstorage.js +++ b/src/www/js/localstorage.js @@ -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;