1 // HTTP.Cookies - Burak Gürsoy <burak[at]cpan[dot]org>
4 I removed all the docs (except author and license info) to reduce download size
5 -bill erickson <billserickson@gmail.com>
8 if (!HTTP) var HTTP = {}; // create base class if undefined
10 HTTP.Cookies = function () { // HTTP.Cookies constructor
11 this.JAR = ''; // data cache
14 HTTP.Cookies.VERSION = '1.01';
16 HTTP.Cookies.Date = function () { // expire time calculation class
22 'M' : 60 * 60 * 24 * 30,
23 'y' : 60 * 60 * 24 * 365
27 HTTP.Cookies.Date.prototype.parse = function (x) {
28 if(!x || x == 'now') return 0;
29 var date = x.match(/^(.+?)(\w)$/i);
31 return (this.is_num(date[1]) && (of = this.is_date(date[1],date[2]))) ? of : 0;
34 HTTP.Cookies.Date.prototype.is_date = function (num, x) {
35 if (!x || x.length != 1) return 0;
37 return (ar = x.match(/^(s|m|h|d|w|M|y)$/) ) ? num * 1000 * this.format[ar[0]] : 0;
40 HTTP.Cookies.Date.prototype.is_num = function (x) {
41 if (x.length == 0) return;
43 for (var i = 0; i < x.length; i++) {
44 if ("0123456789.-+".indexOf(x.charAt(i)) == -1) {
52 HTTP.Cookies.prototype.date = new HTTP.Cookies.Date; // date object instance
54 // Get the value of the named cookie. Usage: password = cookie.read('password');
55 HTTP.Cookies.prototype.read = function (name) {
59 var array = document.cookie.split(';');
60 for (var x = 0; x < array.length; x++) {
61 var pair = array[x].split('=');
62 if(pair[0].substring (0,1) == ' ') pair[0] = pair[0].substring(1, pair[0].length);
66 this.JAR[pair[0]] = pair[1];
69 for(var cookie in this.JAR) {
71 value = this.JAR[cookie];
75 return value ? unescape(value) : '';
78 // Create a new cookie or overwrite existing. Usage: cookie.write('password', 'secret', '1m');
79 HTTP.Cookies.prototype.write = function (name, value, expires, path, domain, secure) {
81 if (!expires) expires = '';
82 if (expires == '_epoch') {
83 expires = new Date(0);
84 } else if (expires != -1) {
86 Now.setTime(Now.getTime() + this.date.parse(expires));
87 expires = Now.toGMTString();
89 if(expires) extra += "; expires=" + expires;
90 if(path ) extra += "; path=" + path;
91 if(domain ) extra += "; domain=" + domain;
92 if(secure ) extra += "; secure=" + secure;
93 document.cookie = name + "=" + escape(value) + extra;
96 // Delete the named cookie. Usage: cookie.remove('password');
97 HTTP.Cookies.prototype.remove = function (name, path, domain, secure) {
98 this.write(name, '', '_epoch', path, domain, secure);
105 HTTP.Cookies - JavaScript class for reading, writing and deleting cookies
109 Burak Gürsoy, E<lt>burakE<64>cpan.orgE<gt>
113 Copyright 2005 Burak Gürsoy. All rights reserved.
117 This library is free software; you can redistribute it and/or modify
118 it under the terms of the "Artistic License":
119 L<http://dev.perl.org/licenses/artistic.html>.