Results 1 to 3 of 3

Thread: I'm really confused.

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default I'm really confused.

    My script may have some errors, but right now my problem are a couple of lines.
    Code:
    			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.
    Last edited by ???; 08-25-2007 at 04:25 AM. Reason: Bad spelling

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    .constructor doesn't exist in some browsers. Try instead:
    Code:
    if(expireDate instanceof Date)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    It worked in deleteCookie, but not in setCookie.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •