- uploading 1.1 in tags
[mootools.git] / Remote / Cookie.js
blobea24b14c2bf085c7a34b29ae189e0b3af19167a1
1 /*
2 Script: Cookie.js
3         A cookie reader/creator
5 Credits:
6         based on the functions by Peter-Paul Koch (http://quirksmode.org)
7 */
9 /*
10 Class: Cookie
11         Class for creating, getting, and removing cookies.
14 var Cookie = new Abstract({
16         options: {
17                 domain: false,
18                 path: false,
19                 duration: false,
20                 secure: false
21         },
23         /*
24         Property: set
25                 Sets a cookie in the browser.
27         Arguments:
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.
32         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.
39         Returns:
40                 An object with the options, the key and the value. You can give it as first parameter to Cookie.remove.
42         Example:
43                 >Cookie.set("username", "Harald", {duration: 1}); //save this for 1 day
44                 >Cookie.set("username", "JackBauer", {duration: false}); //session cookie
46         */
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();
57                 }
58                 if (options.secure) value += '; secure';
59                 document.cookie = key + '=' + value;
60                 return $extend(options, {'key': key, 'value': value});
61         },
63         /*
64         Property: get
65                 Gets the value of a cookie.
67         Arguments:
68                 key - the name of the cookie you wish to retrieve.
70         Returns:
71                 The cookie string value, or false if not found.
73         Example:
74                 >Cookie.get("username") //returns Aaron
75         */
77         get: function(key){
78                 var value = document.cookie.match('(?:^|;)\\s*' + key.escapeRegExp() + '=([^;]*)');
79                 return value ? decodeURIComponent(value[1]) : false;
80         },
82         /*
83         Property: remove
84                 Removes a cookie from the browser.
86         Arguments:
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>
90         Examples:
91                 >Cookie.remove("username") //bye-bye Aaron
92                 >var myCookie = Cookie.set('user', 'jackbauer', {domain: 'mootools.net'});
93                 >Cookie.remove(myCookie);
94         */
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}));
99         }