Reads and writes a cookie.
- domain - (string: defaults to false) The domain the cookie belongs to.
- path - (string: defaults to '/') The path the cookie belongs to.
- 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.
- secure - (boolean: defaults to false) Stored cookie information can be accessed only from a secure environment.
Writes a cookie in the browser.
Syntax:
var myCookie = Cookie.write(key, value[, options]);
Аргументы:
- key - (string) The key (or name) of the cookie.
- value - (string) The value to set. Cannot contain semicolons.
- options - (mixed, optional) See Cookie.
Возвращает:
- (object) An object with the options, the key and the value. You can give it as first parameter to Cookie.dispose.
Examples:
Saves the cookie for the duration of the session:
var myCookie = Cookie.write('username', 'JackBauer');
Saves the cookie for a day:
var myCookie = Cookie.write('username', 'JackBauer', {duration: 1});
Note:
In order to share the cookie with pages located in a different path, the Cookie.options.domain value must be set.
Reads the value of a cookie.
Syntax:
var myCookie = Cookie.read(name);
Аргументы:
- name - (string) The name of the cookie to read.
Возвращает:
- (mixed) The cookie string value, or null if not found.
Пример:
Cookie.read('username');
Removes a cookie from the browser.
Syntax:
var oldCookie = Cookie.dispose(name[, options]);
Аргументы:
- name - (string) The name of the cookie to remove or a previously saved Cookie instance.
- options - (object, optional) See Cookie.
Examples:
Remove a Cookie:
Cookie.dispose('username'); // Bye-bye JackBauer!
Creating a cookie and removing it right away:
var myCookie = Cookie.write('username', 'JackBauer', {domain: 'mootools.net'});
if (Cookie.read('username') == 'JackBauer') { myCookie.dispose(); }
Credits:
- Based on the functions by Peter-Paul Koch of QuirksMode.