-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie_mgmt.js
128 lines (109 loc) · 3.19 KB
/
cookie_mgmt.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
/*************************************************************************
*
* Cookie Management.
* Created by Martin J. Wolf
* Requires: jQuery
* License: MIT
*
* With this script you can easily store, access and remove multiple values
* in a single cookie.
*
*************************************************************************/
var Cookie = {};
/**
* Setting some defaults.
* @param cName Name of the cookie
* @param xDays Days until cookie expires (optional)
* @param path Path (optional)
*/
Cookie = function (cName, xDays, path)
{
this.cName = cName;
this.xDays = xDays ? xDays : 0;
this.path = path ? path : "/";
};
/**
* Add a parameter or overwrite its value. If the cookie doesn´t exist, it will be created.
* @param cParam Name of the parameter
* @param pValue Value of the parameter
*/
Cookie.prototype.set = function (cParam, pValue)
{
if (typeof pValue != "undefined" && pValue !== null) {
var valObj = {};
valObj[cParam] = pValue;
var curVal = this.get() ? JSON.parse(this.get()) : null;
var newVal = curVal ? $.extend(curVal, valObj) : valObj;
var newCookieVal = btoa(JSON.stringify(newVal));
var expires = "";
if(this.xDays) {
var date = new Date();
date.setTime(date.getTime() + (this.xDays * 24 * 60 * 60 * 1000));
expires = ";expires=" + date.toUTCString();
}
var path = "";
if(this.path) {
path = ";path=" + this.path;
}
document.cookie = this.cName + "=" + newCookieVal + expires + path;
}
};
/**
* Get a cookie value
* @param cParam
* @returns {*}
*/
Cookie.prototype.get = function (cParam)
{
var name = this.cName + "=";
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var c = cookies[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
if (cParam) {
var cValue = JSON.parse(atob(c.substring(name.length, c.length)));
return cValue[cParam];
} else {
return atob(c.substring(name.length, c.length));
}
}
}
return "";
};
/**
* Delete a single parameter of the cookie
* @param cParam
*/
Cookie.prototype.clear = function (cParam)
{
if (this.get()) {
var curVal = JSON.parse(this.get());
delete curVal[cParam];
if ($.isEmptyObject(curVal)) {
this.remove();
} else {
var newCookieVal = JSON.stringify(curVal);
var expires = "";
if(this.xDays) {
var date = new Date();
date.setTime(date.getTime() + (this.xDays * 24 * 60 * 60 * 1000));
expires = ";expires=" + date.toUTCString();
}
var path = "";
if(this.path) {
path = ";path=" + this.path;
}
document.cookie = this.cName + "=" + newCookieVal + expires + path;
}
}
};
/**
* Remove the cookie
*/
Cookie.prototype.remove = function ()
{
document.cookie = this.cName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
};