Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Cookies and OOP

  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cookies and OOP

    The posting of this code was inspired by beetle
    Originally posted by beetle
    <snip />...it never crossed my mind to make a constructor <snip />
    and all those "Cookie Problems" posts that are flooding this Forum:
    Code:
     
    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
    Attached Files Attached Files
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Bristol, UK
    Posts
    932
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...and what's wrong with my cookie script?

    I have posted this more than once, I find it very useful and use it all the time

    Code:
    //----- 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

    ::] krycek [::
    ithium | SOAPI | SDP | PTPScript manual
    "ithium is a non-profit webhost, which is pretty much unique. The mission of ithium is to provide free hosting resources for worthwhile and needy non-profit projects, which otherwise may not be able to obtain such facilities. The money from commercial customers goes to maintain ithium's servers and further development."

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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....
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I have this kicking around my hard drive:
    Code:
    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 .

  • #6
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JKD, I'm not familiar with the syntax you used here:

    Code:
    Cookie: function(name, value, expires, domain, path, secure)
    ...
    fill: function()
    ...
    empty: function()
    ... with the colons and all. Would that be the same as...

    Code:
    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?:

    Code:
    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).

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya Skyzyx, you got it right. What jkd used is called an object literal. The same way this
    Code:
    var nums = [1,2,3,4];
    is the literal syntax for this
    Code:
    var nums = new Array(1,2,3,4);
    For objects, this
    Code:
    var nums = { M: 1000, D:500, C:100, L:50, X:10, V:5, I:1 }
    is the same as doing this
    Code:
    var 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
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #8
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perhaps, I could try something like this...

    Code:
    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...

    Code:
    var bob=new myObject;
    
    bob.fill(1, 2, 3);

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Skyzyx,

    You may be interested in my 'constructor constructor' function I posted here
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #10
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by Skyzyx; 01-25-2003 at 06:52 PM.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #11
    New Coder
    Join Date
    Dec 2003
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Where DO I PUT THOSE COOKIES?

    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?


  •  

    Posting Permissions

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