2 * @fileoverview This file has been modified for ease of use. Please download the original from
3 * http://www.jaaulde.com/test_bed/cookieLib/ for the original (and latest) versions.
5 * @author James Auldridge, Copyright (c) 2005
10 * A singleton Cookie-handling object
12 * Methods: get, set, del, test
15 JSAN.use("MochiKit.Logging");
17 var Cookie = window.Cookie || {};
19 get: function(cookieName) {
20 var cookieNameStart,valueStart,valueEnd,value;
21 cookieNameStart = document.cookie.indexOf(cookieName+'=');
22 if (cookieNameStart < 0) {return null;}
23 valueStart = document.cookie.indexOf(cookieName+'=') + cookieName.length + 1;
24 valueEnd = document.cookie.indexOf(";",valueStart);
25 if (valueEnd == -1){valueEnd = document.cookie.length;}
26 value = document.cookie.substring(valueStart,valueEnd );
27 value = unescape(value);
28 if (value == "") {return null;}
31 set: function(cookieName,value,hoursToLive,path,domain,secure) {
32 var domainRegEx = /[^.]+\.[^.]+$/;
33 domain = window.location.hostname.match(domainRegEx);
34 if(domain == 'cornell.edu') domain = 'sgn.cornell.edu';
35 var expireString,timerObj,expireAt,pathString,domainString,secureString,setCookieString;
36 if (!hoursToLive || typeof hoursToLive != 'number' || parseInt(hoursToLive)=='NaN'){
40 timerObj = new Date();
41 timerObj.setTime(timerObj.getTime()+(parseInt(hoursToLive)*60*60*1000));
42 expireAt = timerObj.toGMTString();
43 expireString = "; expires="+expireAt;
45 pathString = "; path=";
46 (!path || path=="") ? pathString += "/" : pathString += path;
47 domainString = "; domain=";
48 if(!domain || domain==""){
49 domain = window.location.hostname;
51 // if(domain=="localhost") domain = "localhost.localdomain";
52 domainString += domain;
54 (secure === true) ? secureString = "; secure" : secureString = "";
55 value = escape(value);
56 setCookieString = cookieName+"="+value+expireString+pathString+domainString;
57 document.cookie = setCookieString;
59 del: function(cookieName,path,domain){
60 (!path || !path.length) ? path="" : path=path;
61 (!domain || !domain.length) ? domain="" : domain=domain;
62 Cookie.set(cookieName,"",-8760,path,domain);
65 Cookie.set('cT','acc');
66 var runTest = Cookie.get('cT');
67 if (runTest == 'acc'){
78 var test = Cookie.test();
80 MochiKit.Logging.logDebug("Javascript Cookie Setting: Works!");
83 MochiKit.Logging.logError("Javascript Cookie Setting: Failed.");
84 var hn = window.location.hostname;
87 "Cookie failure: You appear to be using a one-word hostname (" + hn + ") without a domain, " +
88 "which won't work with cookies. Try using something like " +
89 "localhost.localdomain, and set /etc/hosts accordingly");