PDA

View Full Version : I'm really confused.



???
08-25-2007, 04:24 AM
My script may have some errors, but right now my problem are a couple of lines.
var Cookie = function Cookie (type,name,value,expireDate,path) {
if (type == "get") {
this.getCookieByName (name);
}
if (type == "set") {
this.setCookie (name,value,expireDate,path);
}
}
Cookie.prototype.name = null;
Cookie.prototype.value = null;
Cookie.prototype.expireDate = null;
Cookie.prototype.path = null;
Cookie.prototype.storeCookie = function storeCookie () {
document.cookie = this.name + "=" + this.value + "; expires=" + this.expireDate + "; path=" + this.path + "; ";
}
Cookie.prototype.setCookie = function setCookie (name,value,expireDate,path) {
this.name = name;
this.value = value;
if (typeof expireDate == "string") {
this.expireDate = expireDate;
}
if (expireDate.constructor == Date) {
this.expireDate = expireDate.toUTCString ();
}
this.path = path;
}
Cookie.prototype.getCookieByName = function getCookieByName (name) {
var nvPairs = document.cookie.split ("; ");
for (var i = 0; i < nvPairs.length; i = i + 1) {
var seped = nvPairs [i].split ("=");
if (seped [0] == name) {
this.name = seped [0];
this.value = seped [1];
}
}
}
Cookie.prototype.getCookieByValue = function getCookieByValue (value) {
var toReturn = new Array ();
var nvPairs = document.cookie.split ("; ");
for (var i = 0; i < nvPairs.length; i = i + 1) {
var seped = nvPairs [i].split ("=");
if (seped [1] == value) {
toReturn [toReturn.length] = new Array ();
toReturn [toReturn.length] [0] = seped [0];
toReturn [toReturn.length] [1] = seped [1];
}
}
return toReturn;
}
Cookie.prototype.deleteCookie = function deleteCookie (path) {
var theDate = new Date ();
theDate.setFullYear (theDate.getFullYear() - 1);
this.setCookie (this.name,this.value,theDate,path);
this.storeCookie ();
this.setCookie (null,null,null,null);
}
var haha = new Cookie ();
haha.setCookie ("ppk1","lolered",new Date ().setFullYear(2010),"/");
haha.storeCookie();
alert(document.cookie);
haha.deleteCookie("/");
alert(document.cookie);
My problem is in setCookie, it says expireDate has no properties. But, in delete, it's expire date does, and I pass delete's expireDate to set. I just don't get it.

Twey
08-25-2007, 01:47 PM
.constructor doesn't exist in some browsers. Try instead:
if(expireDate instanceof Date)

???
08-26-2007, 04:59 PM
It worked in deleteCookie, but not in setCookie.