Files
gitbook/theme/javascript/storage.js
T
2015-10-05 16:47:19 +02:00

29 lines
708 B
JavaScript

var baseKey = '';
/*
* Simple module for storing data in the browser's local storage
*/
module.exports = {
setBaseKey: function(key) {
baseKey = key;
},
set: function(key, value) {
key = baseKey+':'+key;
localStorage[key] = JSON.stringify(value);
},
get: function(key, def) {
key = baseKey+':'+key;
if (localStorage[key] === undefined) return def;
try {
var v = JSON.parse(localStorage[key]);
return v == null ? def : v;;
} catch(err) {
return localStorage[key] || def;
}
},
remove: function(key) {
key = baseKey+':'+key;
localStorage.removeItem(key);
}
};