View Full Version : Cookies and OOP
Vladdy
12-16-2002, 02:30 PM
The posting of this code was inspired by beetle
Originally posted by beetle
<snip />...it never crossed my mind to make a constructor :rolleyes: <snip />
and all those "Cookie Problems" posts that are flooding this Forum:
function cookie(name,value)
{ this.name = name;
this.value = value;
this.attributes = new Array();
this.expiresIn = function (days)
{ expiration = new Date();
expiration.setTime(expiration.getTime() + (days*86400000));
this.attributes["expires"] = expiration.toGMTString();
};
this.set = function ()
{ var cookiestr = this.name + "=" + escape(this.value);
for(attr in this.attributes)
{ cookiestr += "; " + attr
if(this.attributes[attr].length > 0)
cookiestr += "=" + this.attributes[attr];
}
document.cookie=cookiestr;
};
this.erase = function()
{ this.expiresIn(-1);
this.set();
delete cookies[this.name];
};
}
function cookieContainer()
{ this.add=function (name,value)
{ this[name]=new cookie(name,value);
};
this.get=function (name)
{ for(o in this)
if(o == name)
return this[o];
return null;
}
var cstr=document.cookie;
var spaces=/\s/gi;
cstr=cstr.replace(spaces,'');
while(cstr.length>0)
{ cequal=cstr.indexOf("=");
if(cequal==-1) cequal=cstr.length;
var name=cstr.substring(0,cequal);
cstr=cstr.substring(cequal+1,cstr.length);
cend=cstr.indexOf(";");
if(cend==-1) cend=cstr.length;
var value=unescape(cstr.substring(0,cend));
cstr=cstr.substring(cend+1,cstr.length);
this.add(name,value);
}
}
var cookies = new cookieContainer();
Documentation with UELA attached ;)
krycek
12-16-2002, 02:56 PM
...and what's wrong with my cookie script? :D
I have posted this more than once, I find it very useful and use it all the time :)
//----- COOKIE OBJECT ------------------------------------------------------------------------------------------------------------------//
function CookieObject() {
this.cookie = null
this.cookies = new Array()
}
CookieObject.prototype.save = function(name, value, time) {
this.cookie = name + "=" + value
if (time != null) {
var date = new Date()
date.setTime(date.getTime() + (time * 1000))
this.cookie += "; expires=" + date.toGMTString()
}
this.cookie += "; path=/"
document.cookie = this.cookie
}
CookieObject.prototype.read = function(name) {
name += "="
this.cookies = document.cookie.split(";")
for (var i = 0; i < this.cookies.length; i++) {
this.cookie = this.cookies[i]
while (this.cookie.charAt(0) == " ") {
this.cookie = this.cookie.substring(1, this.cookie.length)
}
if (this.cookie.indexOf(name) == 0) {
return this.cookie.substring(name.length, this.cookie.length)
}
}
return null
}
CookieObject.prototype.clear = function(name) {
this.save(name, "", -1)
}
cookie = new CookieObject()
Not so sure what yours does different ;) hehehe but I expect you will point out something that yours does better lol :p
::] krycek [::
Vladdy
12-16-2002, 03:54 PM
Nothing is wrong with yours.
My script has functionality to set the cookie attributes such as path, domain, version.
It parses the document.cookie string once at initialization, which can arguably be more efficient when there are many cookies which are accessed often.
Other than that.... great minds think alike.... :D :D
beetle
12-16-2002, 09:10 PM
Hey now, what's going on here? I inspire people?
hehe....
Thanks for the code fellas, I just might have a use for one of these :D
I have this kicking around my hard drive:
var CookieJar = {
Cookie: function(name, value, expires, domain, path, secure) {
this.name = name;
this.value = value;
this.expires = expires;
this.domain = domain;
this.path = path;
this.secure = secure;
this.toString = function() {
return this.name + '=' + escape(this.value) + ((typeof this.expires != 'undefined' && this.expires.constructor == Date) ? ('; expires=' + this.expires.toUTCString()) : '') + ((typeof this.domain != 'undefined') ? ('; domain=' + this.domain) : '') + ((typeof this.path != 'undefined') ? ('; path=' + this.path) : '') + ((typeof this.secure != 'undefined') ? '; secure' : '');
}
this.bake = function() {
CookieJar.cookies[this.name] = this;
document.cookie = this.toString();
}
this.eat = function () {
delete CookieJar.cookies[this.name];
document.cookie = this.name + '=' + escape(this.value) + '; expires=Thu, 01-Jan-70 00:00:01 GMT';
}
},
fill: function() {
CookieJar.cookies = document.cookie.match(/([^ ;]+=[^;]+(?:; expires=[^;])?(?:; domain=[^;])?(?:; path=[^;])?(?:; secure)?)/g);
var i, name, value, expires, domain, path, split;
for (i = 0; i < this.cookies.length; i++) {
split = CookieJar.cookies[i].split('=');
name = split[0];
value = unescape(split[1].split(';')[0]);
expires = new Date((typeof split[2] == 'undefined') ? undefined : split[2].split(';')[0]);
domain = (typeof split[3] == 'undefined') ? undefined : split[3].split(';')[0];
path = (typeof split[4] == 'undefined') ? undefined : split[4].split(';')[0];
secure = (typeof split[4].split('; ')[1] == 'undefined') ? undefined : true;
CookieJar.cookies[name] = new CookieJar.Cookie(name, value, (isNaN(expires.valueOf()) ? undefined : expires), domain, path, secure);
}
},
empty: function() {
for (var i in CookieJar.cookies) {
if (CookieJar.cookies[i].constructor == CookieJar.Cookie)
CookieJar.cookies[i].eat();
else
delete CookieJar.cookies[i];
}
}
}
CookieJar.fill();
Something I wrote once to mess around with cookies, but never really used.
But it does have playful names :).
Skyzyx
01-11-2003, 11:21 AM
JKD, I'm not familiar with the syntax you used here:
Cookie: function(name, value, expires, domain, path, secure)
...
fill: function()
...
empty: function()
... with the colons and all. Would that be the same as...
this.Cookie=function(name, value, expires, domain, path, secure)
...
this.fill=function()
...
this.empty=function()
?
Also is it possible to pass arguments through an anonymous function within an object like this?:
this.fill(a, b, c)=function(a, b, c)
I was toying with something like this today, and I couldn't get it to work properly (or at all, rather).
beetle
01-11-2003, 11:37 AM
Ya Skyzyx, you got it right. What jkd used is called an object literal. The same way thisvar nums = [1,2,3,4];
is the literal syntax for thisvar nums = new Array(1,2,3,4);
For objects, thisvar nums = { M: 1000, D:500, C:100, L:50, X:10, V:5, I:1 }
is the same as doing thisvar nums = new Object();
nums.M = 1000;
nums.D = 500;
nums.C = 100;
nums.L = 50;
nums.X = 10;
nums.V = 5;
nums.I = 1;Since functions are just another variable type in javascript, jkd is just assigning functions to his CookieJar object's properties to make them methods.
About the parameter passing thing with anon function...that's not really an anonymous function, it's name is fill and you can't pass parameters like that.
Hope I've helped
Skyzyx
01-11-2003, 08:33 PM
Perhaps, I could try something like this...
function fill(a, b, c)
{
this.a=a;
this.b=b;
this.c=c;
// Do a, b, c stuff...
}
function myObject()
{
this.fill=fill;
// Do this.a, this.b, this.c stuff...
}
Would this idea work to produce some thing that works like this...
var bob=new myObject;
bob.fill(1, 2, 3);
beetle
01-11-2003, 09:43 PM
Skyzyx,
You may be interested in my 'constructor constructor' function I posted here (http://www.sitepointforums.com/showthread.php?s=&threadid=90644)
Skyzyx
01-25-2003, 06:50 PM
Here's something I was able to throw together.
/*************************
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?
stoney
03-27-2004, 07:41 PM
I've got a script that was made for me by a guy that told me Adios. I loved the little thing, except there's one problemo...that be...every time the images reload there's a chance of one or two of those images from the previous load popping up in the table of ten images. (BTW)
So : here's the script: Where do I put the cookies, so I don't see these images repeat every other time or whatever?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.