Post by Eric on Aug 24, 2005 20:58:29 GMT
Here 'tis:
function Cookie()
{
this.main = document.cookie;
if(arguments.length > 1){
if(arguments[0] == "make"){
var props = new Array('name','val','expires','path','domain','secure');
for(var m=1;arguments[m];m++){
this[props[m-1]] = arguments[m];
}
for(m--;props[m];m++){
this[props[m]] = 0;
}
}
if(arguments[0] == "read"){
this.rName = arguments[1];
this.rVal = '';
}
}
}
Cookie.prototype.getVal = function()
{
if(this.rName){
var curName = ' ' + this.rName + "=";
var cookieMatch = new RegExp(curName + '(.+?)(;|$)', 'i');
if(this.main.match(cookieMatch))
this.rVal = RegExp.$1.replace('smcln',';');
}
}
Cookie.prototype.setCookie = function()
{
if(this.name && this.val){
var addVal = this.val.replace(';','smcln');
var curAdd = this.name + '=' + addVal + ';';
curAdd += (this.expires)? 'expires=' + this.expires +';' : '';
curAdd += (this.path)? 'path=' + this.path + ';' : '';
curAdd += (this.domain)? 'domain=' + this.domain + ';' : '';
curAdd += (this.secure)? 'secure;' : '';
document.cookie = curAdd;
}
}
A bit on using it:
To read a cookie:
var inst = new Cookie('read','nameOfCookie');
inst.getVal();
inst.rVal now holds the cookie value.
To make a cookie:
var makeC = new Cookie('make','cookieName','valueToSet',expires,path,domain,secure);
makeC.setCookie();
You can leave out any values after 'valueToSet'. If you don't want to set one of the ones after that, but one past it, then put a 0 in place of what you would normally put there.
secure is a true or false value, and expires can be a date or string, and all the rest have to be strings as far as I know.
Shin kindly pointed out that I left out the delete cookie option which I will do shortly, unless someone else would like to.
function Cookie()
{
this.main = document.cookie;
if(arguments.length > 1){
if(arguments[0] == "make"){
var props = new Array('name','val','expires','path','domain','secure');
for(var m=1;arguments[m];m++){
this[props[m-1]] = arguments[m];
}
for(m--;props[m];m++){
this[props[m]] = 0;
}
}
if(arguments[0] == "read"){
this.rName = arguments[1];
this.rVal = '';
}
}
}
Cookie.prototype.getVal = function()
{
if(this.rName){
var curName = ' ' + this.rName + "=";
var cookieMatch = new RegExp(curName + '(.+?)(;|$)', 'i');
if(this.main.match(cookieMatch))
this.rVal = RegExp.$1.replace('smcln',';');
}
}
Cookie.prototype.setCookie = function()
{
if(this.name && this.val){
var addVal = this.val.replace(';','smcln');
var curAdd = this.name + '=' + addVal + ';';
curAdd += (this.expires)? 'expires=' + this.expires +';' : '';
curAdd += (this.path)? 'path=' + this.path + ';' : '';
curAdd += (this.domain)? 'domain=' + this.domain + ';' : '';
curAdd += (this.secure)? 'secure;' : '';
document.cookie = curAdd;
}
}
A bit on using it:
To read a cookie:
var inst = new Cookie('read','nameOfCookie');
inst.getVal();
inst.rVal now holds the cookie value.
To make a cookie:
var makeC = new Cookie('make','cookieName','valueToSet',expires,path,domain,secure);
makeC.setCookie();
You can leave out any values after 'valueToSet'. If you don't want to set one of the ones after that, but one past it, then put a 0 in place of what you would normally put there.
secure is a true or false value, and expires can be a date or string, and all the rest have to be strings as far as I know.
Shin kindly pointed out that I left out the delete cookie option which I will do shortly, unless someone else would like to.