Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
![image](http://upstatement.com/wp-content/uploads/2012/01/jquery-total-storage1.png)
Dead-Simple local storage for every browser and device

## What makes it TOTAL Storage?
Expand Down Expand Up @@ -41,4 +40,4 @@ Last year my firm worked on a project to help seniors calculate their savings, s
### Many thanks to:
Andris Reinman and Klaus Hartl. Their plugins (jStorage and Cookie, respectively) provided the roadmap to make this. Also feedback and comments from users have been most helpful in making this more efficient and killing some bugs.

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/jarednova/jquery-total-storage/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-total-storage",
"version": "1.0.0",
"version": "1.1.3",
"description": "Local storage for every browser",
"license": "MIT",
"ignore": [
Expand Down
264 changes: 132 additions & 132 deletions jquery.total-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*
* 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.
*
* @name $.totalStorage
* @cat Plugins/Cookie
* @author Jared Novack/jared@upstatement.com
* @version 1.1.3
* @url http://upstatement.com/blog/2012/01/jquery-local-storage-done-right-and-easy/
*/

/**
Expand All @@ -34,149 +40,143 @@
$.totalStorage('people', myArray);
//to return:
$.totalStorage('people');
*
* @name $.totalStorage
* @cat Plugins/Cookie
* @author Jared Novack/jared@upstatement.com
* @version 1.1.2
* @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;
}
}
;(function ($, undefined) {

/* Variables I'll need throughout */

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 */

$.totalStorage = function(key, value, options){
return $.totalStorage.impl.init(key, value);
};
$.totalStorage = function (key, value, options) {
return $.totalStorage.impl.init(key, value, options);
};

$.totalStorage.setItem = function(key, value){
return $.totalStorage.impl.setItem(key, value);
};
$.totalStorage.setItem = function (key, value, options) {
return $.totalStorage.impl.setItem(key, value, options);
};

$.totalStorage.getItem = function(key){
return $.totalStorage.impl.getItem(key);
};
$.totalStorage.getItem = function (key) {
return $.totalStorage.impl.getItem(key);
};

$.totalStorage.getAll = function(){
return $.totalStorage.impl.getAll();
};
$.totalStorage.getAll = function () {
return $.totalStorage.impl.getAll();
};

$.totalStorage.deleteItem = function(key){
return $.totalStorage.impl.deleteItem(key);
};
$.totalStorage.deleteItem = function (key) {
return $.totalStorage.impl.deleteItem(key);
};

/* 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 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<pairs.length; i++){
var pair = pairs[i].split('=');
var key = pair[0];
items.push({key:key, value:this.parseResult($.cookie(key))});
}
} catch(e){
return null;
}
} else {
for (var j in ls){
if (j.length){
items.push({key:j, value:this.parseResult(ls.getItem(j))});
}
}
}
return items;
},
parseResult: function(res){
var ret;
try {
ret = JSON.parse(res);
if (typeof ret == 'undefined'){
ret = res;
}
if (ret == 'true'){
ret = true;
}
if (ret == 'false'){
ret = false;
}
if (parseFloat(ret) == ret && typeof ret != "object"){
ret = parseFloat(ret);
}
} catch(e){
ret = res;
}
return ret;
}
};
$.totalStorage.impl = {

init: function (key, value, options) {
if (typeof value != 'undefined') {
return this.setItem(key, value, options);
} else {
return this.getItem(key);
}
},

setItem: function (key, value, options) {
if (!supported) {
try {
$.cookie(key, value, options);
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 < pairs.length; i++) {
var pair = pairs[i].split('=');
var key = pair[0];
items.push({key: key, value: this.parseResult($.cookie(key))});
}
} catch (e) {
return null;
}
} else {
for (var j in ls) {
if (j.length) {
items.push({key: j, value: this.parseResult(ls.getItem(j))});
}
}
}
return items;
},
parseResult: function (res) {
var ret;
try {
ret = JSON.parse(res);
if (typeof ret == 'undefined') {
ret = res;
}
if (ret == 'true') {
ret = true;
}
if (ret == 'false') {
ret = false;
}
if (parseFloat(ret) == ret && typeof ret != "object") {
ret = parseFloat(ret);
}
} catch (e) {
ret = res;
}
return ret;
}
};
})(jQuery);
19 changes: 11 additions & 8 deletions jquery.total-storage.min.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
/*
/**
* 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,
* 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.
*
* @name $.totalStorage
* @cat Plugins/Cookie
* @author Jared Novack/jared@upstatement.com
* @version 1.1.2
* @version 1.1.3
* @url http://upstatement.com/blog/2012/01/jquery-local-storage-done-right-and-easy/
*/

(function(c,h){var e,d;if("localStorage"in window)try{d="undefined"===typeof window.localStorage?h:window.localStorage,e="undefined"==typeof d||"undefined"==typeof window.JSON?!1:!0}catch(j){e=!1}c.totalStorage=function(b,a){return c.totalStorage.impl.init(b,a)};c.totalStorage.setItem=function(b,a){return c.totalStorage.impl.setItem(b,a)};c.totalStorage.getItem=function(b){return c.totalStorage.impl.getItem(b)};c.totalStorage.getAll=function(){return c.totalStorage.impl.getAll()};c.totalStorage.deleteItem=
function(b){return c.totalStorage.impl.deleteItem(b)};c.totalStorage.impl={init:function(b,a){return"undefined"!=typeof a?this.setItem(b,a):this.getItem(b)},setItem:function(b,a){if(!e)try{return c.cookie(b,a),a}catch(g){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 f=JSON.stringify(a);d.setItem(b,f);return this.parseResult(f)},getItem:function(b){if(!e)try{return this.parseResult(c.cookie(b))}catch(a){return null}b=
d.getItem(b);return this.parseResult(b)},deleteItem:function(b){if(!e)try{return c.cookie(b,null),!0}catch(a){return!1}d.removeItem(b);return!0},getAll:function(){var b=[];if(e)for(var a in d)a.length&&b.push({key:a,value:this.parseResult(d.getItem(a))});else try{var g=document.cookie.split(";");for(a=0;a<g.length;a++){var f=g[a].split("=")[0];b.push({key:f,value:this.parseResult(c.cookie(f))})}}catch(h){return null}return b},parseResult:function(b){var a;try{a=JSON.parse(b),"undefined"==typeof a&&
(a=b),"true"==a&&(a=!0),"false"==a&&(a=!1),parseFloat(a)==a&&"object"!=typeof a&&(a=parseFloat(a))}catch(c){a=b}return a}}})(jQuery);
!function(t,e){var o,r,n="test"
if("localStorage"in window)try{r="undefined"==typeof window.localStorage?e:window.localStorage,o="undefined"!=typeof r&&"undefined"!=typeof window.JSON,window.localStorage.setItem(n,"1"),window.localStorage.removeItem(n)}catch(t){o=!1}t.totalStorage=function(e,o,r){return t.totalStorage.impl.init(e,o,r)},t.totalStorage.setItem=function(e,o,r){return t.totalStorage.impl.setItem(e,o,r)},t.totalStorage.getItem=function(e){return t.totalStorage.impl.getItem(e)},t.totalStorage.getAll=function(){return t.totalStorage.impl.getAll()},t.totalStorage.deleteItem=function(e){return t.totalStorage.impl.deleteItem(e)},t.totalStorage.impl={init:function(t,e,o){return"undefined"!=typeof e?this.setItem(t,e,o):this.getItem(t)},setItem:function(e,n,a){if(!o)try{return t.cookie(e,n,a),n}catch(t){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 l=JSON.stringify(n)
return r.setItem(e,l),this.parseResult(l)},getItem:function(e){if(!o)try{return this.parseResult(t.cookie(e))}catch(t){return null}var n=r.getItem(e)
return this.parseResult(n)},deleteItem:function(e){if(!o)try{return t.cookie(e,null),!0}catch(t){return!1}return r.removeItem(e),!0},getAll:function(){var e=[]
if(o)for(var n in r)n.length&&e.push({key:n,value:this.parseResult(r.getItem(n))})
else try{for(var a=document.cookie.split(";"),l=0;l<a.length;l++){var i=a[l].split("="),u=i[0]
e.push({key:u,value:this.parseResult(t.cookie(u))})}}catch(t){return null}return e},parseResult:function(t){var e
try{e=JSON.parse(t),"undefined"==typeof e&&(e=t),"true"==e&&(e=!0),"false"==e&&(e=!1),parseFloat(e)==e&&"object"!=typeof e&&(e=parseFloat(e))}catch(o){e=t}return e}}}(jQuery)