forked from savetheclocktower/javascript-stuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.js
96 lines (81 loc) · 2.1 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
//= require <prototype>
if (!Prototype || Prototype.Version.indexOf('1.6') !== 0) {
throw "This script requires Prototype >= 1.6.";
}
Object.isDate = function(object) {
return object instanceof Date;
};
/**
* class Cookie
* Creates a cookie.
**/
var Cookie = Class.create({
/**
* new Cookie(name, value[, expires])
*
* - name (String): The name of the cookie.
* - value (String): The value of the cookie.
* - expires (Number | Date): Exact date (or number of days from now) that
* the cookie will expire.
**/
initialize: function(name, value, expires) {
if (Object.isNumber(expires)) {
var days = expires;
expires = new Date();
expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
}
if (Object.isDate(expires))
expires = expires.toGMTString();
if (!Object.isUndefined(expires) && expires !== "")
expires = "; expires=" + expires;
this.name = name;
this.value = value;
this.expires = expires;
document.cookie = name + "=" + value + expires + "; path=/";
},
toString: function() {
return this.value;
},
inspect: function() {
return "#<Cookie #{name}:#{value}>".interpolate(this);
}
});
/**
* namespace Cookie
**/
Object.extend(Cookie, {
/**
* Cookie.set(name, value, expires)
*
* Alias of [[Cookie#initialize]].
**/
set: function(name, value, expires) {
return new Cookie(name, value, expires);
},
/**
* Cookie.get(name)
*
* Returns the value of the cookie with the given name.
* - name (String): The name of the cookie to retrieve.
**/
get: function(name) {
var c = document.cookie.split(';');
for (var i = 0, cookie; i < c.length; i++) {
cookie = c[i].split('=');
if (cookie[0].strip() === name)
return cookie[1].strip();
}
return null;
},
/**
* Cookie.unset(name)
*
* Deletes a cookie.
* - name (String): The name of the cookie to delete.
*
**/
unset: function(name) {
return Cookie.set(name, "", -1);
}
});
Cookie.erase = Cookie.unset;