Rename <Type>.from to <Type>.convert
[mootools.git] / Docs / Utilities / Cookie.md
blob23016021e3c0e17af20229470a9ef8342644090c
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.
17 ### Syntax:
19         var myCookie = Cookie.write(key, value[, options]);
21 ### Arguments:
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][].
27 ### Returns:
29 * (*object*) An object with the options, the key and the value. You can give it as first parameter to [Cookie.dispose][].
31 ### Examples:
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});
41 ### Note:
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.
49 ### Syntax:
51         var myCookie = Cookie.read(name);
53 ### Arguments:
55 1. name - (*string*) The name of the cookie to read.
57 ### Returns:
59 * (*mixed*) The cookie string value, or null if not found.
61 ### Example:
63         Cookie.read('username');
65 ## Cookie Method: dispose {#Cookie:dispose}
67 Removes a cookie from the browser.
69 ### Syntax:
71         var oldCookie = Cookie.dispose(name[, options]);
73 ### Arguments:
75 1. name - (*string*) The name of the cookie to remove or a previously saved Cookie instance.
76 2. options - (*object*, optional) See [Cookie][].
78 ### Examples:
80 Remove a 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(); }
89 ### Credits:
91 - Based on the functions by Peter-Paul Koch of [QuirksMode][].
93 [Cookie.dispose]: #Cookie:dispose
94 [Cookie.options]: #Cookie-options
95 [Cookie]: #Cookie
96 [QuirksMode]: http://www.quirksmode.org