Fixing development version number.
[mootools.git] / Docs / Utilities / Cookie.md
blobcb1fa6f1f072749a5604e559923865711f40d9d3
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.
16 ### Syntax:
18         var myCookie = Cookie.write(key, value[, options]);
20 ### Arguments:
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][].
26 ### Returns:
28 * (*object*) An object with the options, the key and the value. You can give it as first parameter to [Cookie.dispose][].
30 ### Examples:
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});
40 ### Note:
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.
48 ### Syntax:
50         var myCookie = Cookie.read(name);
52 ### Arguments:
54 1. name - (*string*) The name of the cookie to read.
56 ### Returns:
58 * (*mixed*) The cookie string value, or null if not found.
60 ### Example:
62         Cookie.read('username');
64 ## Cookie Method: dispose {#Cookie:dispose}
66 Removes a cookie from the browser.
68 ### Syntax:
70         var oldCookie = Cookie.dispose(name[, options]);
72 ### Arguments:
74 1. name - (*string*) The name of the cookie to remove or a previously saved Cookie instance.
75 2. options - (*object*, optional) See [Cookie][].
77 ### Examples:
79 Remove a 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(); }
88 ### Credits:
90 - Based on the functions by Peter-Paul Koch of [QuirksMode][].
92 [Cookie.dispose]: #Cookie:dispose
93 [Cookie.options]: #Cookie-options
94 [Cookie]: #Cookie
95 [QuirksMode]: http://www.quirksmode.org