/** * TotalStorage * * Copyright (c) 2012 Jared Novack & Upstatement (upstatement.com) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * Total Storage is the conceptual the love child of jStorage by Andris Reinman, * and Cookie by Klaus Hartl -- though this is not connected to either project. */ /** * Create a local storage parameter * == What makes it TOTAL Storage? == * The browser doesn't support local storage it will fall-back to cookies! (Using the wonderful $.cookie plugin). * Send it strings, numbers even complex object arrays! TotalStorage does not care. Your efforts to defeat it will prove futile. * Simple as shit. jStorage and some other very well-written plugins provide a bevy of options for expiration, security and so forth. Frequently this is more power than you need and vulnerable to confusion if you're just want it to work (JWITW) * @desc Set the value of a key to a string * @example $.totalStorageFPP('the_key', 'the_value'); * @desc Set the value of a key to a number * @example $.totalStorageFPP('the_key', 800.2); * @desc Set the value of a key to a complex Array * @example var myArray = new Array(); * myArray.push({name:'Jared', company:'Upstatement', zip:63124}); myArray.push({name:'McGruff', company:'Police', zip:60652}; $.totalStorageFPP('people', myArray); //to return: $.totalStorageFPP('people'); * * @name $.totalStorageFPP * @cat Plugins/Cookie * @author Jared Novack/jared@upstatement.com * @version 1.1.2 OPC * @url http://upstatement.com/blog/2012/01/jquery-local-storage-done-right-and-easy/ */ ;(function($, undefined){ /* Variables I'll need throghout */ var supported, ls, mod = 'test'; 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; } window.localStorage.setItem(mod, '1'); window.localStorage.removeItem(mod); } catch (err){ supported = false; } } /* Make the methods public */ $.totalStorageFPP = function(key, value, options){ return $.totalStorageFPP.impl.init(key, value); }; $.totalStorageFPP.setItem = function(key, value){ return $.totalStorageFPP.impl.setItem(key, value); }; $.totalStorageFPP.getItem = function(key){ return $.totalStorageFPP.impl.getItem(key); }; $.totalStorageFPP.getAll = function(){ return $.totalStorageFPP.impl.getAll(); }; $.totalStorageFPP.deleteItem = function(key){ return $.totalStorageFPP.impl.deleteItem(key); }; /* Object to hold all methods: public and private */ $.totalStorageFPP.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 true; } catch(e){ return false; } } ls.removeItem(key); return true; }, getAll: function(){ var items = []; if (!supported){ try { var pairs = document.cookie.split(";"); for (var i = 0; i