3 A cookie reader/creator
6 based on the functions by Peter-Paul Koch (http://quirksmode.org)
11 Class for creating, getting, and removing cookies.
14 var Cookie = new Abstract({
25 Sets a cookie in the browser.
28 key - the key (name) for the cookie
29 value - the value to set, cannot contain semicolons
30 options - an object representing the Cookie options. See Options below. Default values are stored in Cookie.options.
33 domain - the domain the Cookie belongs to. If you want to share the cookie with pages located on a different domain, you have to set this value. Defaults to the current domain.
34 path - the path the Cookie belongs to. If you want to share the cookie with pages located in a different path, you have to set this value, for example to "/" to share the cookie with all pages on the domain. Defaults to the current path.
35 duration - the duration of the Cookie before it expires, in days.
36 If set to false or 0, the cookie will be a session cookie that expires when the browser is closed. This is default.
37 secure - Stored cookie information can be accessed only from a secure environment.
40 An object with the options, the key and the value. You can give it as first parameter to Cookie.remove.
43 >Cookie.set("username", "Harald", {duration: 1}); //save this for 1 day
44 >Cookie.set("username", "JackBauer", {duration: false}); //session cookie
48 set: function(key, value, options){
49 options = $merge(this.options, options);
50 value = encodeURIComponent(value);
51 if (options.domain) value += '; domain=' + options.domain;
52 if (options.path) value += '; path=' + options.path;
53 if (options.duration){
54 var date = new Date();
55 date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
56 value += '; expires=' + date.toGMTString();
58 if (options.secure) value += '; secure';
59 document.cookie = key + '=' + value;
60 return $extend(options, {'key': key, 'value': value});
65 Gets the value of a cookie.
68 key - the name of the cookie you wish to retrieve.
71 The cookie string value, or false if not found.
74 >Cookie.get("username") //returns Aaron
78 var value = document.cookie.match('(?:^|;)\\s*' + key.escapeRegExp() + '=([^;]*)');
79 return value ? decodeURIComponent(value[1]) : false;
84 Removes a cookie from the browser.
87 cookie - the name of the cookie to remove or a previous cookie (for domains)
88 options - optional. you can also pass the domain and path here. Same as options in <Cookie.set>
91 >Cookie.remove("username") //bye-bye Aaron
92 >var myCookie = Cookie.set('user', 'jackbauer', {domain: 'mootools.net'});
93 >Cookie.remove(myCookie);
96 remove: function(cookie, options){
97 if ($type(cookie) == 'object') this.set(cookie.key, '', $merge(cookie, {duration: -1}));
98 else this.set(cookie, '', $merge(options, {duration: -1}));