forked from pouetnet/pouet2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.js
66 lines (58 loc) · 1.96 KB
/
cookie.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
// by Jason McCreary October 10, 2008
// http://jason.pureconcepts.net/2008/10/javascript_cookie_object/
var Cookie = {
data: {},
options: {expires: 1, domain: "", path: "", secure: false},
init: function(options, data) {
Cookie.options = Object.extend(Cookie.options, options || {});
var payload = Cookie.retrieve();
if(payload) {
Cookie.data = payload.evalJSON();
}
else {
Cookie.data = data || {};
}
Cookie.store();
},
getData: function(key) {
return Cookie.data[key];
},
setData: function(key, value) {
Cookie.data[key] = value;
Cookie.store();
},
removeData: function(key) {
delete Cookie.data[key];
Cookie.store();
},
retrieve: function() {
var start = document.cookie.indexOf(Cookie.options.name + "=");
if(start == -1) {
return null;
}
if(Cookie.options.name != document.cookie.substr(start, Cookie.options.name.length)) {
return null;
}
var len = start + Cookie.options.name.length + 1;
var end = document.cookie.indexOf(';', len);
if(end == -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(len, end));
},
store: function() {
var expires = '';
if (Cookie.options.expires) {
var today = new Date();
expires = Cookie.options.expires * 86400000;
expires = ';expires=' + new Date(today.getTime() + expires);
}
document.cookie = Cookie.options.name + '=' + escape(Object.toJSON(Cookie.data)) + Cookie.getOptions() + expires;
},
erase: function() {
document.cookie = Cookie.options.name + '=' + Cookie.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
},
getOptions: function() {
return (Cookie.options.path ? ';path=' + Cookie.options.path : '') + (Cookie.options.domain ? ';domain=' + Cookie.options.domain : '') + (Cookie.options.secure ? ';secure' : '');
}
};