-
Notifications
You must be signed in to change notification settings - Fork 25
/
jquery.cookies.js
119 lines (102 loc) · 2.35 KB
/
jquery.cookies.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
"use strict";
(function ($,undefined) {
function get (name) {
var cookies = {};
if (document.cookie) {
var values = document.cookie.split(/; */g);
for (var i = 0; i < values.length; ++ i) {
var value = values[i];
var pos = value.search("=");
var key, value;
if (pos < 0) {
key = decodeURIComponent(value);
value = undefined;
}
else {
key = decodeURIComponent(value.slice(0,pos));
value = decodeURIComponent(value.slice(pos+1));
}
cookies[key] = value;
}
}
if (name === undefined) {
return cookies;
}
else {
return cookies[name];
}
}
function set (name, value, expires, path, domain, secure) {
switch (arguments.length) {
case 1:
for (var key in name) {
set(key, name[key]);
}
return;
case 2:
if (value && typeof(value) === "object") {
expires = value.expires;
path = value.path;
domain = value.domain;
secure = value.secure;
value = value.value;
}
}
if (value === null || value === undefined) {
expires = -1;
}
var buf = [encodeURIComponent(name)+'='+encodeURIComponent(value)];
switch (typeof(expires)) {
case "string":
expires = new Date(expires);
case "object":
buf.push("expires="+expires.toUTCString());
break;
case "boolean":
if (expires) {
break;
}
expires = 365*2000;
case "number":
var date = new Date();
date.setTime(date.getTime()+(1000*60*60*24*expires));
buf.push("expires="+date.toUTCString());
break;
}
if (path === true) {
buf.push("path="+document.location.pathname);
}
else if (path !== undefined && path !== false) {
buf.push("path="+path.replace(/[;\s]/g,encodeURIComponent));
}
if (domain === true) {
buf.push("domain="+document.location.host);
}
else if (domain !== undefined && domain !== false) {
buf.push("domain="+domain.replace(/[;\s]/g,encodeURIComponent));
}
if (secure) {
buf.push("secure");
}
document.cookie = buf.join("; ");
}
$.cookie = function () {
switch (arguments.length) {
case 0:
return get();
case 1:
if (typeof(arguments[0]) !== "object") {
return get(arguments[0]);
}
case 2:
case 3:
case 4:
case 5:
case 6:
set.apply(this,arguments);
return this;
default:
throw new Error("Illegal number of arguments");
}
};
})(jQuery);