6 description: Class for creating, reading, and deleting browser Cookies.
8 license: MIT-style license.
11 - Based on the functions by Peter-Paul Koch (http://quirksmode.org).
13 requires: [Options, Browser]
20 var Cookie = new Class({
33 initialize: function(key, options){
35 this.setOptions(options);
38 write: function(value){
39 if (this.options.encode) value = encodeURIComponent(value);
40 if (this.options.domain) value += '; domain=' + this.options.domain;
41 if (this.options.path) value += '; path=' + this.options.path;
42 if (this.options.duration){
43 var date = new Date();
44 date.setTime(date.getTime() + this.options.duration * 24 * 60 * 60 * 1000);
45 value += '; expires=' + date.toGMTString();
47 if (this.options.secure) value += '; secure';
48 this.options.document.cookie = this.key + '=' + value;
53 var value = this.options.document.cookie.match('(?:^|;)\\s*' + this.key.escapeRegExp() + '=([^;]*)');
54 return (value) ? decodeURIComponent(value[1]) : null;
58 new Cookie(this.key, Object.merge({}, this.options, {duration: -1})).write('');
64 Cookie.write = function(key, value, options){
65 return new Cookie(key, options).write(value);
68 Cookie.read = function(key){
69 return new Cookie(key).read();
72 Cookie.dispose = function(key, options){
73 return new Cookie(key, options).dispose();