-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cookies.dart
45 lines (40 loc) · 1014 Bytes
/
Cookies.dart
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
/*
* dart document.cookie lib
*
* ported from
* http://www.quirksmode.org/js/cookies.html
*
*/
#library('cookies');
#import('dart:html');
class Cookies {
Cookies() {
}
void createCookie(String name, String value, int days) {
String expires;
if (days != null) {
Date now = new Date.now();
Date date = new Date.fromEpoch(now.value + days*24*60*60*1000, new TimeZone.local());
expires = '; expires=' + date.toString();
} else {
Date then = new Date.fromEpoch(0, new TimeZone.utc());
expires = '; expires=' + then.toString();
}
document.cookie = name + '=' + value + expires + '; path=/';
}
String readCookie(String name) {
String nameEQ = name + '=';
List<String> ca = document.cookie.split(';');
for (int i = 0; i < ca.length; i++) {
String c = ca[i];
c = c.trim();
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length);
}
}
return null;
}
void eraseCookie(String name) {
createCookie(name, '', null);
}
}