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.
11 * httpOnly - (*boolean*: defaults to false) Stored cookie information can be accessed only on the server.
13 ## Cookie Method: write {#Cookie:write}
15 Writes a cookie in the browser.
19 var myCookie = Cookie.write(key, value[, options]);
23 1. key - (*string*) The key (or name) of the cookie.
24 2. value - (*string*) The value to set. Cannot contain semicolons.
25 3. options - (*mixed*, optional) See [Cookie][].
29 * (*object*) An object with the options, the key and the value. You can give it as first parameter to [Cookie.dispose][].
33 Saves the cookie for the duration of the session:
35 var myCookie = Cookie.write('username', 'JackBauer');
37 Saves the cookie for a day:
39 var myCookie = Cookie.write('username', 'JackBauer', {duration: 1});
43 In order to share the cookie with pages located in a different path, the [Cookie.options.domain][Cookie.options] value must be set.
45 ## Cookie Method: read {#Cookie:read}
47 Reads the value of a cookie.
51 var myCookie = Cookie.read(name);
55 1. name - (*string*) The name of the cookie to read.
59 * (*mixed*) The cookie string value, or null if not found.
63 Cookie.read('username');
65 ## Cookie Method: dispose {#Cookie:dispose}
67 Removes a cookie from the browser.
71 var oldCookie = Cookie.dispose(name[, options]);
75 1. name - (*string*) The name of the cookie to remove or a previously saved Cookie instance.
76 2. options - (*object*, optional) See [Cookie][].
82 Cookie.dispose('username'); // Bye-bye JackBauer!
84 Creating a cookie and removing it right away:
86 var myCookie = Cookie.write('username', 'JackBauer', {domain: 'mootools.net'});
87 if (Cookie.read('username') == 'JackBauer') { myCookie.dispose(); }
91 - Based on the functions by Peter-Paul Koch of [QuirksMode][].
93 [Cookie.dispose]: #Cookie:dispose
94 [Cookie.options]: #Cookie-options
96 [QuirksMode]: http://www.quirksmode.org