-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.js
144 lines (120 loc) · 3.36 KB
/
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
@Name: sb.cookies
@Author: Paul Visco
@Version: 1.3 07/08/08
@Description: Used to handle cookies - which can set values between client visits
*/
sb.cookies ={
path : '/',
domain : '',
onlog : '',
/**
@Name: sb.cookies.get
@Description: Used to get cookie values
@Param: String name The name of the cookie who's value you are trying to get
@Return: String Returns the value stored for the cookie or false if the cookie is not found
@Example:
sb.cookies.get('myCookie', 'paul');
*/
get : function(name){
var i,n, parts = document.cookie.split(';');
for(i=0;i<parts.length;i++){
n = parts[i].split('=');
n[0] = n[0].replace(/ /, "");
if(name==n[0]){
return unescape(n[1]);
}
}
return false;
},
/**
@Name: sb.cookies.set
@Description: Used to make the clients computer set a value as a cookie
@Param: String name The name (key) of the cookie which will hold the valuee
@Param: String value The value the cookie holds, set cookies are limited to <4k
@Param: Days number The number of days to rememeber the value for. If not set they become session cookies and expire when the user closes the browser
@Example:
sb.cookies.set('name', 'paul');
sb.cookies.set('name', 'paul', 30);
*/
set : function(name, value, days){
var ck, date = new Date();
var exp = '';
if(days){
date.setTime(date.getTime()+(days*24*60*60*1000));
exp = '; expires='+date.toGMTString();
}
ck=name+'=' + escape(value) + exp + '; path=' + sb.cookies.path + ';' + ' host=' + sb.cookies.domain;
document.cookie = ck;
},
/**
@Name: sb.cookies.forget
@Description: Alias for sb.cookies.clear to maintain backwards compat
@Param: String name The name (key) of the cookie which will be forgotten
@Example:
sb.cookies.forget('myCookie');
*/
forget : function(name){
return this.clear(name);
},
/**
@Name: sb.cookies.unset
@Description: Alias for sb.cookies.clear to maintain backwards compat
@Param: String name The name (key) of the cookie which will be forgotten
@Example:
sb.cookies.forget('myCookie');
*/
unset : function(name){
return this.clear(name);
},
/**
@Name: sb.cookies.clear
@Description: Used to make the clients computer forget a cookie
@Param: String name The name (key) of the cookie which will be forgotten
@Example:
sb.cookies.clear('myCookie');
*/
clear : function(name){
this.set(name, "", -1);
},
/**
@Name: sb.cookies.forgetAll
@Description: Forgets all cookies stored for your domain
@Example:
sb.cookies.forgetAll();
*/
forgetAll : function(){
return this.clearAll();
},
/**
@Name: sb.cookies.clearAll
@Description: Clears all cookies stored for your domain
@Example:
sb.cookies.clearAll();
*/
clearAll : function(){
var n,i,deleted =[], parts = document.cookie.split(';');
for(i=0;i<parts.length;i++){
n = parts[i].split('=');
if(n[0] !== undefined){
deleted.push(n[0]);
this.set(n[0], "", -1);
}
}
},
/**
@Name: sb.cookies.listAll
@Description: Used Internally
*/
listAll : function(){
var i, c, list=[], parts = document.cookie.split(';');
for(i=0;i<parts.length;i++){
c = parts[i].split('=');
list.push(c[0].replace(/ /, ""));
}
return list;
},
typeOf : function(){
return 'sb.cookies';
}
};