1 # Object: Cookie {#Cookie}
3 Reads and writes a cookie.
5 ## Options: {#Cookie-options}
7 * domain - (*string*: defaults to false) The domain the cookie belongs to.
8 * path - (*string*: defaults to '/') The path the cookie belongs to.
9 * duration - (*number*: defaults to false) The duration of the cookie (in days) before it expires. If set to false or 0, the cookie will be a session cookie that expires when the browser is closed.
10 * secure - (*boolean*: defaults to false) Stored cookie information can be accessed only from a secure environment.
12 ## Cookie Method: write {#Cookie:write}
14 Writes a cookie in the browser.
18 var myCookie = Cookie.write(key, value[, options]);
22 1. key - (*string*) The key (or name) of the cookie.
23 2. value - (*string*) The value to set. Cannot contain semicolons.
24 3. options - (*mixed*, optional) See [Cookie][].
28 * (*object*) An object with the options, the key and the value. You can give it as first parameter to [Cookie.dispose][].
32 Saves the cookie for the duration of the session:
34 var myCookie = Cookie.write('username', 'JackBauer');
36 Saves the cookie for a day:
38 var myCookie = Cookie.write('username', 'JackBauer', {duration: 1});
42 In order to share the cookie with pages located in a different path, the [Cookie.options.domain][Cookie.options] value must be set.
44 ## Cookie Method: read {#Cookie:read}
46 Reads the value of a cookie.
50 var myCookie = Cookie.read(name);
54 1. name - (*string*) The name of the cookie to read.
58 * (*mixed*) The cookie string value, or null if not found.
62 Cookie.read('username');
64 ## Cookie Method: dispose {#Cookie:dispose}
66 Removes a cookie from the browser.
70 var oldCookie = Cookie.dispose(name[, options]);
74 1. name - (*string*) The name of the cookie to remove or a previously saved Cookie instance.
75 2. options - (*object*, optional) See [Cookie][].
81 Cookie.dispose('username'); // Bye-bye JackBauer!
83 Creating a cookie and removing it right away:
85 var myCookie = Cookie.write('username', 'JackBauer', {domain: 'mootools.net'});
86 if (Cookie.read('username') == 'JackBauer') { myCookie.dispose(); }
90 - Based on the functions by Peter-Paul Koch of [QuirksMode][].
92 [Cookie.dispose]: #Cookie:dispose
93 [Cookie.options]: #Cookie-options
95 [QuirksMode]: http://www.quirksmode.org