-
Notifications
You must be signed in to change notification settings - Fork 863
/
cookie.js
110 lines (95 loc) · 3.3 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
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
/*
Copyright 2012, KISSY UI Library v1.40dev
MIT Licensed
build time: Dec 7 00:21
*/
/**
* @ignore
* @fileOverview cookie
* @author lifesinger@gmail.com
*/
KISSY.add('cookie', function (S) {
var doc = S.Env.host.document,
MILLISECONDS_OF_DAY = 24 * 60 * 60 * 1000,
encode = encodeURIComponent,
decode = S.urlDecode;
function isNotEmptyString(val) {
return (typeof val == 'string') && val !== '';
}
/**
* Provide Cookie utilities.
* @class KISSY.Cookie
* @singleton
*/
return S.Cookie = {
/**
* Returns the cookie value for given name
* @return {String} name The name of the cookie to retrieve
*/
get: function (name) {
var ret, m;
if (isNotEmptyString(name)) {
if ((m = String(doc.cookie).match(
new RegExp('(?:^| )' + name + '(?:(?:=([^;]*))|;|$)')))) {
ret = m[1] ? decode(m[1]) : '';
}
}
return ret;
},
/**
* Set a cookie with a given name and value
* @param {String} name The name of the cookie to set
* @param {String} val The value to set for cookie
* @param {Number|Date} expires
* if Number specified how many days this cookie will expire
* @param {String} domain set cookie's domain
* @param {String} path set cookie's path
* @param {Boolean} secure whether this cookie can only be sent to server on https
*/
set: function (name, val, expires, domain, path, secure) {
var text = String(encode(val)), date = expires;
// 从当前时间开始,多少天后过期
if (typeof date === 'number') {
date = new Date();
date.setTime(date.getTime() + expires * MILLISECONDS_OF_DAY);
}
// expiration date
if (date instanceof Date) {
text += '; expires=' + date.toUTCString();
}
// domain
if (isNotEmptyString(domain)) {
text += '; domain=' + domain;
}
// path
if (isNotEmptyString(path)) {
text += '; path=' + path;
}
// secure
if (secure) {
text += '; secure';
}
doc.cookie = name + '=' + text;
},
/**
* Remove a cookie from the machine by setting its expiration date to sometime in the past
* @param {String} name The name of the cookie to remove.
* @param {String} domain The cookie's domain
* @param {String} path The cookie's path
* @param {String} secure The cookie's secure option
*/
remove: function (name, domain, path, secure) {
this.set(name, '', -1, domain, path, secure);
}
};
});
/*
2012.02.14 yiminghe@gmail.com
- jsdoc added
2010.04
- get 方法要考虑 ie 下,
值为空的 cookie 为 'test3; test3=3; test3tt=2; test1=t1test3; test3', 没有等于号。
除了正则获取,还可以 split 字符串的方式来获取。
- api 设计上,原本想借鉴 jQuery 的简明风格:S.cookie(name, ...), 但考虑到可扩展性,目前
独立成静态工具类的方式更优。
*/