This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjquery.Storage.js
131 lines (116 loc) · 3.59 KB
/
jquery.Storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* jquery.Storage
* A jQuery plugin to make localStorage easy and managable to use
*
* Copyright (c) Brandon Hicks (Tarellel)
*
* Version: 1.0.0a (12/6/10)
* Requires: jQuery 1.4
*
*
*/
(function(jQuery) {
// validate if the visiting browser supports localStorage
var supported = true;
var keyMeta = 'ls_';
//var localStorage === window.localStorage
if (typeof localStorage == 'undefined' || typeof JSON == 'undefined'){
supported = false;
}
// errors produced by localStorage
this.storageError = function(error){
switch(error){
// current browser/device is not supported
case 'SUPPORTED':
alert("Your browser does not support localStorage!");
break;
// browsers database quota is full
case 'QUOTA':
alert("Your storage quota is currently full!");
console.log("Browser database quote exceeded.");
break;
// Any other error that may have occurred
default:
alert('An unknown error has occurred!');
break;
}
return true;
};
// saves specified item using ("key","value")
this.saveItem = function(itemKey, itemValue, lifetime){
if (typeof lifetime == 'undefined'){
lifetime = 60000;
}
if (!supported){
// set future expiration for cookie
dt = new Date();
// 1 = 1day can use days variable
//dt.setTime(dt.getTime() + (1*24*60*60*1000));
dt.setTime(dt.getTime() + lifetime);
expires = "expires= " + dt.toGMTString();
document.cookie = keyMeta + itemKey.toString() + "=" + itemValue + "; " + expires + "; path=/";
return true;
}
// set specified item
try{
localStorage.setItem(keyMeta+itemKey.toString(), JSON.stringify(itemValue));
} catch (e){
// if the browsers database is full produce error
if (e == QUOTA_EXCEEDED_ERR) {
this.storageError('QUOTA');
return false;
}
}
return true;
};
// load value of a specified database item
this.loadItem = function(itemKey){
if(itemKey===null){ return null; }
if (!supported){
var cooKey = keyMeta + itemKey.toString() + "=";
// go through cookies looking for one that matchs the specified key
var cookArr = document.cookie.split(';');
for(var i=0, cookCount = cookArr; i < cookCount; i++){
var current_cookie = cookArr[i];
while(current_cookie.charAt(0) == ''){
current_cookie = current_cookie.substring(1, current_cookie.length);
// if keys match return cookie
if (current_cookie.indexOf(cooKey) == 0) {
return current_cookie.substring(cooKey.length, current_cookie.length);
}
}
}
return null;
}
var data = localStorage.getItem(keyMeta+itemKey.toString());
if (data){
return JSON.parse(data);
}else{
return false;
}
};
// removes specified item
this.deleteItem = function (itemKey){
if (!supported){
document.cookie = keyMeta + itemKey.toString() + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
return true;
}
localStorage.removeItem(keyMeta+itemKey.toString());
return true;
};
// WARNING!!! this clears entire localStorage Database
this.deleteAll = function(){
if (!supported){
// process each and every cookie through a delete funtion
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++){
this.deleteItem(cookies[i].split("=")[0]);
}
return true;
}
localStorage.clear();
return true;
};
// jquery namespace for the function set
jQuery.Storage = this;
})(jQuery);