JSFiddle - React, Tailwind, and code Playground
by rossmartin
JavaScript
;(function($, undefined){
/* Variables I'll need throghout */
var supported, ls;
if ('localStorage' in window){
try {
ls = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage;
if (typeof ls == 'undefined' || typeof window.JSON == 'undefined'){
supported = false;
} else {
supported = true;
}
}
catch (err){
supported = false;
}
}
/* Make the methods public */
$.totalStorage = function(key, value, options){
return $.totalStorage.impl.init(key, value);
};
$.totalStorage.setItem = function(key, value){
return $.totalStorage.impl.setItem(key, value);
};
$.totalStorage.getItem = function(key){
return $.totalStorage.impl.getItem(key);
};
$.totalStorage.getAll = function(){
return $.totalStorage.impl.getAll();
};
$.totalStorage.deleteItem = function(key){
return $.totalStorage.impl.deleteItem(key);
};
$.totalStorage.removeAll = function(){
return $.totalStorage.impl.removeAll();
};
/* Object to hold all methods: public and private */
$.totalStorage.impl = {
init: function(key, value){
if (typeof value != 'undefined') {
return this.setItem(key, value);
} else {
return this.getItem(key);
}
},
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);
},
getItem: function(key){
if (!supported){
try {
return this.parseResult($.cookie(key));
} catch(e){
return null;
}
}
var item = ls.getItem(key);
return this.parseResult(item);
},
deleteItem: function(key){
if (!supported){
try {
$.cookie(key, null);
return...