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] ) ;
Arguments:
key - (string ) The key (or name) of the cookie.
value - (string ) The value to set. Cannot contain semicolons.
options - (mixed , optional) See Cookie .
Returns:
(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 ) ;
Arguments:
name - (string ) The name of the cookie to read.
Returns:
(mixed ) The cookie string value, or null if not found.
Example:
Cookie.read ( 'username' ) ;
Removes a cookie from the browser.
Syntax:
var oldCookie = Cookie.dispose ( name [ , options] ) ;
Arguments:
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 .
Please enable JavaScript to view the comments powered by Disqus.
comments powered by