Here's something I was able to throw together.
Code:
/*************************
COOKIES
Based on "Night of the Living Cookie" by Bill Dortch
Released to the public domain.
This version (c) 2003, Skyzyx Technologies
*************************/
function cookie(name, value, expires, path, domain, secure)
{
// Passed Values
this.name=name;
this.value=value;
this.expires=expires;
this.path=path;
this.domain=domain;
this.secure=secure;
// Read cookie
this.read=function()
{
arg = this.name + "=";
alen = arg.length;
clen = document.cookie.length;
i = 0;
while (i < clen)
{
j = i + alen;
if (document.cookie.substring(i, j) == arg)
{
endstr = document.cookie.indexOf (";", j);
if (endstr == -1) endstr = document.cookie.length;
return unescape(document.cookie.substring(j, endstr));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
// Set cookie
this.set=function()
{
// Store initial value of "this.expires" for re-initialization.
expStore=this.expires;
// Set time to absolute zero.
exp = new Date();
base = new Date(0);
skew = base.getTime();
if (skew > 0) exp.setTime (exp.getTime() - skew);
exp.setTime(exp.getTime() + (this.expires*24*60*60*1000));
this.expires=exp;
document.cookie = this.name + "=" + escape (this.value) +
((this.expires) ? "; expires=" + this.expires.toGMTString() : "") +
((this.path) ? "; path=" + this.path : "") +
((this.domain) ? "; domain=" + this.domain : "") +
((this.secure) ? "; secure" : "");
// Re-initialize
this.expires=expStore;
}
// Kill cookie
this.kill=function()
{
document.cookie = this.name + "=" +
((this.path) ? "; path=" + this.path : "") +
((this.domain) ? "; domain=" + this.domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
// Change cookie settings.
this.changeName=function(chName) { this.kill(); this.name=chName; this.set(); } // No practical use.
this.changeVal=function(chVal) { this.kill(); this.value=chVal; this.set(); }
this.changeExp=function(chExp) { this.kill(); this.expires=chExp; this.set(); }
this.changePath=function(chPath) { this.kill(); this.path=chPath; this.set(); }
this.changeDom=function(chDom) { this.kill(); this.domain=chDom; this.set(); }
this.changeSec=function(chSec) { this.kill(); this.secure=chSec; this.set(); }
}
You create a cookie object like this. Anything other than Name and Value is optional. "Expires" is in days.:
myCookie=('Name', 'Value', 365, '/', 'Domain.com', 'Secure');
These are the main functions:
myCookie.set();
myCookie.read();
myCookie.kill();
... And you can change values on the fly without re-initializing the cookie.
myCookie.changeVal('New Value');
... etc.
Whaddya guys think?