Has anyone written any "cookie" class using prototype? Basically, what I am looking for is if there is an easy way to store mutiple cookies in a single cookie using hash or something - easy writing and retrieval (updating the cookie value). For eg - If I had 25 cookies for my domain 5 of the cookies would either get dropped or not be set (as there is a limit of 20 cookies per domain). For such cases, I would actually want to combine hypothetically 5 cookies into one - the cookie could be called global cookie and it''s value is a hash like (name: mandy, email:mandiv@hot..) and so on. All this can be done using the conventional ways but just wondering if someone has made it easy working with cookies :) Let me know your thoughts. Thanks and Regards, Mandy. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On Wednesday 11 January 2006 10:35, Maninder, Singh wrote:> Has anyone written any "cookie" class using prototype? > > Basically, what I am looking for is if there is an easy way to store > mutiple cookies in a single cookie using hash or something - easy writing > and retrieval (updating the cookie value). > > For eg - > > If I had 25 cookies for my domain 5 of the cookies would either get dropped > or not be set (as there is a limit of 20 cookies per domain). For such > cases, I would actually want to combine hypothetically 5 cookies into one - > the cookie could be called global cookie and it''s value is a hash like > (name: mandy, email:mandiv@hot..) and so on. > > All this can be done using the conventional ways but just wondering if > someone has made it easy working with cookies :)you could store the cookie as a JSON object, however I''m not sure exactly how much data you''re allowed to store in a cookie. Also, I know there are HttpOnly cookies, but I don''t know if there are client only cookies, so you have to consider the bandwidth that will be used by the client sending the JSON object to the server as a cookie with every request. I use http://www.crockford.com/JSON/json.js to handle JSON on the javascript side of things. It''s very easy to use and pretty small. YMMV :) -Jeremy -- Jeremy Kitchen ++ kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org In the beginning was The Word and The Word was Content-type: text/plain -- The Word of Bob. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 11/01/06, Maninder, Singh <mandiv-W2hqgAdRMsX2eFz/2MeuCQ@public.gmane.org> wrote:> Basically, what I am looking for is if there is an easy way to store mutiple > cookies in a single cookie using hash or something - easy writing and > retrieval (updating the cookie value).How about this for a start: /* ** ++ Methods to use with Cookiejar: ** ** var kekse = new Cookiejar(cookieJarName, CookiePath) ** This is the constructor.cookieJarName is the name of the collection of ** cookies you would like to associate with this object. This is the name ** of the cookie that holds your name:value pairs ** ** kekse.setCookie(name, value) ** Sets name to value ** ** kekse.getCookie(name) ** Returns the value of name ** ** kekse.cookieExists(name) ** Returns false if name does not exist in the jar. ** Returns the position of the element in the jar if it does exist ** ** kekse.delCookie(name) ** Removes name from the jar ** ** kekse.setExpiration(years, days, hours, minutes, seconds, milliseconds) ** Set the jar to expire in years + days + hours + minutes + seconds + milliseconds ** ** kekse.reset() ** Removes all cookies from the jar ** ** ** */ var Cookiejar = Class.create(); Cookiejar.prototype = { jar: {}, jarname: "jar", expiration: 1 * 365 * 24 * 60 * 60 * 1000, dividerElement: "#", dividerIdValue: ":", path: "", domain: null, secure: false, initialize: function(jarname, path) { this.jarname = jarname; this.path = path; this._read(); }, cookieExists: function(c_id) { if (c_id == "") return false; return !isUndefined(this.jar[c_id]); }, delCookie: function(c_id) { delete this.jar[c_id]; this._write(); }, setCookie: function(c_id, c_value) { this.jar[c_id] = c_value; this._write(); }, getCookie: function(c_id) { return this.jar[c_id] || null; }, reset: function() { this.jar = {}; this._Cookie.remove(this.jarname, this.path, this.domain); }, setExpiration: function(years, days, hours, mins, secs, mill) { this.expiration (((years) ? (years) : 1) * 365 * 24 * 60 * 60 * 1000) + (((days) ? (days) : 365) * 24 * 60 * 60 * 1000) + (((hours) ? (hours) : 24) * 60 * 60 * 1000) + (((mins) ? (mins) : 60) * 60 * 1000 ) + (((secs) ? (secs) : 60) * 1000) + ((mill) ? (mill) : 1000); this._write(); }, _read: function() { var cookiestring = Cookie.get(this.jarname) || ""; cookiestring.split(this.dividerElement).each( function(pair){ pair = pair.split(this.dividerIdValue); this.jar[pair[0]] = pair[1]; }.bind(this) ); }, _write: function() { var dateObj = new Date(); dateObj.setTime(dateObj.getTime() + this.expiration); var base = new Date(0); var skew = base.getTime(); if (skew > 0) dateObj.setTime(dateObj.getTime() - skew); var cookiestring = ""; for (var i in this.jar){ cookiestring += this.dividerElement + encodeURIComponent(i) + this.dividerIdValue + encodeURIComponent(this.jar[i]); } this._Cookie.set(this.jarname, cookiestring, dateObj, this.path, this.domain, this.secure); }, _Cookie = { get: function(name){ if(isString(document.cookie)){ var start = document.cookie.indexOf(name+"="); var len = start+name.length+1; if ((!start) && (name != document.cookie.substring(0,name.length))){ return null; } if (start == -1) return null; var end = document.cookie.indexOf(";",len); if (end == -1) end = document.cookie.length; return decodeURIComponent(document.cookie.substring(len,end)); } else { /* document.cookie is not a string so return an empty string. When tested this will type-convert to boolean false (accurately) giving the impression that client-side cookies are not available on this system:- */ return ""; } }, set: function(name, value, expires, path, domain, secure) { if(isString(document.cookie)){ document.cookie = name + "=" + encodeURIComponent(value) + ( (expires) ? ";expires=" + expires.toGMTString() : "") + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ( (secure) ? ";secure" : ""); }//else document.cookie is not a string so do not write to it. }, remove: function(name, path, domain) { if (this.get(name)) { document.cookie = name + "=" + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-70 00:00:01 GMT"; } } }; }
Actually, scratch the previous post, use this one: /* ** ++ Methods to use with Cookiejar: ** ** var kekse = new Cookiejar(cookieJarName, CookiePath) ** This is the constructor.cookieJarName is the name of the collection of ** cookies you would like to associate with this object. This is the name ** of the cookie that holds your name:value pairs ** ** kekse.setCookie(name, value) ** Sets name to value ** ** kekse.getCookie(name) ** Returns the value of name ** ** kekse.cookieExists(name) ** Returns false if name does not exist in the jar. ** Returns the position of the element in the jar if it does exist ** ** kekse.delCookie(name) ** Removes name from the jar ** ** kekse.setExpiration(years, days, hours, minutes, seconds, milliseconds) ** Set the jar to expire in years + days + hours + minutes + seconds + milliseconds ** ** kekse.reset() ** Removes all cookies from the jar ** ** ** */ var Cookiejar = Class.create(); Cookiejar.prototype = { jar: {}, jarname: "jar", expiration: 1 * 365 * 24 * 60 * 60 * 1000, dividerElement: "#", dividerIdValue: ":", path: "", domain: null, secure: false, initialize: function(jarname, path) { this.jarname = jarname; this.path = path; this._read(); }, cookieExists: function(c_id) { if (c_id == "") return false; return !isUndefined(this.jar[c_id]); }, delCookie: function(c_id) { delete this.jar[c_id]; this._write(); }, setCookie: function(c_id, c_value) { this.jar[c_id] = c_value; this._write(); }, getCookie: function(c_id) { return this.jar[c_id] || null; }, reset: function() { this.jar = {}; Cookie.remove(this.jarname, this.path, this.domain); }, setExpiration: function(years, days, hours, mins, secs, mill) { this.expiration (((years) ? (years) : 1) * 365 * 24 * 60 * 60 * 1000) + (((days) ? (days) : 365) * 24 * 60 * 60 * 1000) + (((hours) ? (hours) : 24) * 60 * 60 * 1000) + (((mins) ? (mins) : 60) * 60 * 1000 ) + (((secs) ? (secs) : 60) * 1000) + ((mill) ? (mill) : 1000); this._write(); }, _read: function() { var cookiestring = Cookie.get(this.jarname) || ""; cookiestring.split(this.dividerElement).each( function(pair){ pair = pair.split(this.dividerIdValue); this.jar[pair[0]] = pair[1]; }.bind(this) ); }, _write: function() { var dateObj = new Date(); dateObj.setTime(dateObj.getTime() + this.expiration); var base = new Date(0); var skew = base.getTime(); if (skew > 0) dateObj.setTime(dateObj.getTime() - skew); var cookiestring = ""; for (var i in this.jar){ cookiestring += this.dividerElement + encodeURIComponent(i) + this.dividerIdValue + encodeURIComponent(this.jar[i]); } Cookie.set(this.jarname, cookiestring, dateObj, this.path, this.domain, this.secure); } }; var Cookie = { get: function(name){ if(isString(document.cookie)){ var start = document.cookie.indexOf(name+"="); var len = start+name.length+1; if ((!start) && (name != document.cookie.substring(0,name.length))){ return null; } if (start == -1) return null; var end = document.cookie.indexOf(";",len); if (end == -1) end = document.cookie.length; return decodeURIComponent(document.cookie.substring(len,end)); } else { /* document.cookie is not a string so return an empty string. When tested this will type-convert to boolean false (accurately) giving the impression that client-side cookies are not available on this system:- */ return ""; } }, set: function(name, value, expires, path, domain, secure) { if(isString(document.cookie)){ document.cookie = name + "=" + encodeURIComponent(value) + ( (expires) ? ";expires=" + expires.toGMTString() : "") + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ( (secure) ? ";secure" : ""); }//else document.cookie is not a string so do not write to it. }, remove: function(name, path, domain) { if (this.get(name)) { document.cookie = name + "=" + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-70 00:00:01 GMT"; } } };
This is a good start for me Martin. Going through it now... First shot at running it gave me errors for isString being not defined. I changed it for both get & set methods FROM: if(isString(document.cookie)){ TO: if(typeof (document.cookie) == ''string''){ This seems to have stopped the error now. Looking into the usage. Thank you, Mandy.
One more: Changed - return !isUndefined(this.jar[c_id]); To return !(typeof (this.jar[c_id]) == ''undefined''); Maybe you had functions for all these but forgot to include them in your email :) Thank you, Mandy. -----Original Message----- From: Maninder, Singh Sent: Thursday, January 12, 2006 9:34 PM To: ''rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org'' Subject: RE: [Rails-spinoffs] Prototype & Cookies This is a good start for me Martin. Going through it now... First shot at running it gave me errors for isString being not defined. I changed it for both get & set methods FROM: if(isString(document.cookie)){ TO: if(typeof (document.cookie) == ''string''){ This seems to have stopped the error now. Looking into the usage. Thank you, Mandy.
Also, Martin - your script depends on the prototype library. But, for smaller applications there might be no need to include the entire prototype.js file. Can you let me know (before I try figuring it out mysef) what functions I need to keep from prototype.js file to make this work. I want to strip down the file for the cookie use. BTW, the script is fantastic. Thank you, Mandy.
Are you familiar with this one: http://www.dustindiaz.com/top-ten-javascript/ see the cookie script a little further down on that site. -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Maninder, Singh Sent: Thursday, January 12, 2006 5:45 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails-spinoffs] Prototype & Cookies Also, Martin - your script depends on the prototype library. But, for smaller applications there might be no need to include the entire prototype.js file. Can you let me know (before I try figuring it out mysef) what functions I need to keep from prototype.js file to make this work. I want to strip down the file for the cookie use. BTW, the script is fantastic. Thank you, Mandy. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Hi Marco, It''s not about just setting a cookie, getting its value & deleting a cookie. That script is easy. What Martin''s script does is super cool.. It makes you define a cookieJar for eg Cookiejar = ''scriptaculous''. Now you can store all scriptaculous related cookies (name/value pairs) into one scriptaculous jar. You can then retrieve these cookies whenever required, delete some cookies from the jar add new ones, etc. This script is much more than a simple get/set cookie script :) Thank you, Mandy.
Martin - Sorry for a million mails on this subject, but I have one suggestion to make this script more perfect. The cookie data limit is 4KB including the cookiename. So, for this case it does make sense to check the limit since this is a cookieJar and can get filled up pretty quickly. Do you think you should put a check for the char limit and if it''s = 4KB, no more cookies would be allowed to set? I haven''t tried what would happen if this limit is exceeded, but just a thought. Let me know what you think. Thank you, Mandy.