1 // HTTP.Cookies - Burak Gürsoy <burak[at]cpan[dot]org>
2 if (!HTTP) var HTTP = {}; // create base class if undefined
4 HTTP.Cookies = function () { // HTTP.Cookies constructor
5 this.JAR = ''; // data cache
8 HTTP.Cookies.VERSION = '1.01';
10 HTTP.Cookies.Date = function () { // expire time calculation class
16 'M' : 60 * 60 * 24 * 30,
17 'y' : 60 * 60 * 24 * 365
21 HTTP.Cookies.Date.prototype.parse = function (x) {
22 if(!x || x == 'now') return 0;
23 var date = x.match(/^(.+?)(\w)$/i);
25 return (this.is_num(date[1]) && (of = this.is_date(date[1],date[2]))) ? of : 0;
28 HTTP.Cookies.Date.prototype.is_date = function (num, x) {
29 if (!x || x.length != 1) return 0;
31 return (ar = x.match(/^(s|m|h|d|w|M|y)$/) ) ? num * 1000 * this.format[ar[0]] : 0;
34 HTTP.Cookies.Date.prototype.is_num = function (x) {
35 if (x.length == 0) return;
37 for (var i = 0; i < x.length; i++) {
38 if ("0123456789.-+".indexOf(x.charAt(i)) == -1) {
46 HTTP.Cookies.prototype.date = new HTTP.Cookies.Date; // date object instance
48 // Get the value of the named cookie. Usage: password = cookie.read('password');
49 HTTP.Cookies.prototype.read = function (name) {
53 var array = document.cookie.split(';');
54 for (var x = 0; x < array.length; x++) {
55 var pair = array[x].split('=');
56 if(pair[0].substring (0,1) == ' ') pair[0] = pair[0].substring(1, pair[0].length);
60 this.JAR[pair[0]] = pair[1];
63 for(var cookie in this.JAR) {
65 value = this.JAR[cookie];
69 return value ? unescape(value) : '';
72 // Create a new cookie or overwrite existing. Usage: cookie.write('password', 'secret', '1m');
73 HTTP.Cookies.prototype.write = function (name, value, expires, path, domain, secure) {
75 if (!expires) expires = '';
76 if (expires == '_epoch') {
77 expires = new Date(0);
78 } else if (expires != -1) {
80 Now.setTime(Now.getTime() + this.date.parse(expires));
81 expires = Now.toGMTString();
83 if(expires) extra += "; expires=" + expires;
84 if(path ) extra += "; path=" + path;
85 if(domain ) extra += "; domain=" + domain;
86 if(secure ) extra += "; secure=" + secure;
87 document.cookie = name + "=" + escape(value) + extra;
90 // Delete the named cookie. Usage: cookie.remove('password');
91 HTTP.Cookies.prototype.remove = function (name, path, domain, secure) {
92 this.write(name, '', '_epoch', path, domain, secure);
99 HTTP.Cookies - JavaScript class for reading, writing and deleting cookies
103 var cookie = new HTTP.Cookies;
104 var password = cookie.read('password');
105 var lastvisit = cookie.read('lastvisit');
106 cookie.write('lastvisit',1079383075,'+1y');
107 cookie.remove('password');
111 HTTP.Cookies is a class for http cookies manipulation. It defines
112 three object methods to read, write and remove cookies. Implementation
113 is somehow similar to the Perl module CGI.pm' s C<cookie()> method.
119 Reads the cookie named C<name> and returns it's value or an empty
122 =head2 write NAME, VALUE [, EXPIRES, PATH, DOMAIN, SECURE]
124 Creates a new cookie with C<NAME> and C<VALUE>. Optional C<EXPIRES>
125 value sets the cookie lifetime.
127 Expire date format: you can use negative or positive numbers combined
128 with 's', 'm', 'h', 'd', 'w', 'M', 'y' or you can use 'now' to
129 expire as soon as possible. Meanings:
140 for a session cookie; pass "-1" as the expires value.
142 Optional parameter C<DOMAIN> can be used to define the domain
143 for which the HTTP cookie is valid.
145 Optional parameter C<EXPIRES> can be used to make it a secure cookie
146 (secure cookies can only be used with HTTPS protocol).
148 =head2 remove NAME [, PATH, DOMAIN, SECURE]
150 Deletes/removes the named cookie from the client.
156 Contact the author if you find any.
158 This library is tested with: Opera 8.01, MSIE 6.0,
159 Netscape Communicator 4.77, Mozilla 1.7.8
160 and Mozilla FireFox 1.0.4 under Windows XP Professional SP2.
164 Burak Gürsoy, E<lt>burakE<64>cpan.orgE<gt>
168 Copyright 2005 Burak Gürsoy. All rights reserved.
172 This library is free software; you can redistribute it and/or modify
173 it under the terms of the "Artistic License":
174 L<http://dev.perl.org/licenses/artistic.html>.