Re: [jQuery] Cookie handling in JQuery
I have a cookie library that I've mostly ported to jQuery. The library silently handles the 4096 boundary allowing the storage of arbitrary strings lengths. It can also be used to store and restore object data. I'll finish the port and post it this weekend for any interested. johnk Klaus Hartl wrote: Yehuda Katz schrieb: It doesn't have to do with DOM Scripting, but then, neither does $.ajax. It's a commonly enough required feature that obviously doesn't belong in the core, but would be nice to tack on to an existing jQuery installation as a plugin without worrying about incompatibilities. Here is a class based on scripts at quirksmode.org, just needs to be ported to jQuery. If nobody does it, I will make a plugin out of it: var CookieHandler = new function() { /** * Reads a cookie. * * @param name the name of the cookie * * @return the value of the cookie * * @author Klaus Hartl (28.06.2006) */ this.get = function(name) { var nameEQ = name + '='; var ca = document.cookie.split(';'); for (var i = 0; i ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); } } return null; }; /** * Sets a cookie with the given name and value and other optional parameters. * * @param namethe name of the cookie * @param value the value of the cookie * @param expires an integer specifying the expiration date from now on *in days. If you set the number of days to 0 the cookie *is trashed when the user closes the browser. * @param pathpath where the cookie is valid (default: path of *calling document). * @param domain domain where the cookie is valid (default: domain of *calling document). * @param secure boolean value indicating if the cookie transmission *requires a secure transmission. * * @author Klaus Hartl (28.06.2006) */ this.set = function(name, value, expires, path, domain, secure) { if (typeof expires == 'number') { var date = new Date(); date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toGMTString(); } else { expires = ''; } path = (path) ? '; path=' + path : ''; domain = (domain) ? '; domain=' + domain : ''; secure = (secure) ? '; secure' : ''; document.cookie = name + '=' + value + expires + path + domain + secure; }; /** * Deletes a cookie by setting the expiry date in the past. * * @param name the name of the cookie * * @author Klaus Hartl (28.06.2006) */ this.erase = function(name) { this.set(name, '', -1); }; }; ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/Cookie-handling-in-JQuery-tf2256520.html#a6332370 Sent from the JQuery forum at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
A cookie handler.On 9/12/06, Rey Bango [EMAIL PROTECTED] wrote: What would you like to see in a plugin?Rey...Yehuda Katz wrote: Would def. make a nice plugin. On 9/11/06, *Rey Bango* [EMAIL PROTECTED] mailto: [EMAIL PROTECTED] wrote: Does JQuery have any methods specific to working with cookies (eg: set, get, et al) or should I just one of the many premade functions out in the wild? Rey... ___ jQuery mailing list discuss@jquery.com mailto: discuss@jquery.com http://jquery.com/discuss/ -- Yehuda Katz Web Developer (ph)718.877.1325 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/___jQuery mailing list discuss@jquery.comhttp://jquery.com/discuss/-- Yehuda KatzWeb Developer(ph)718.877.1325 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
On 12/09/06, Rey Bango [EMAIL PROTECTED] wrote: Does JQuery have any methods specific to working with cookies (eg: set, get, et al) or should I just one of the many premade functions out in the wild? Rey... Do cookies really have much to do with jQuery? Unless a plugin is needed to attach cookies to jQuery objects (for storing view state or events for example) - $(#mydiv).eventsFromCookie(cookiename).click(foo).eventsToCookie(cookiename) ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
It doesn't have to do with DOM Scripting, but then, neither does $.ajax. It's a commonly enough required feature that obviously doesn't belong in the core, but would be nice to tack on to an existing jQuery installation as a plugin without worrying about incompatibilities. -- YehudaOn 9/13/06, Sam Collett [EMAIL PROTECTED] wrote: On 12/09/06, Rey Bango [EMAIL PROTECTED] wrote: Does JQuery have any methods specific to working with cookies (eg: set, get, et al) or should I just one of the many premade functions out in the wild? Rey...Do cookies really have much to do with jQuery? Unless a plugin isneeded to attach cookies to jQuery objects (for storing view state orevents for example) - $(#mydiv).eventsFromCookie(cookiename).click(foo).eventsToCookie(cookiename)___jQuery mailing list discuss@jquery.comhttp://jquery.com/discuss/-- Yehuda KatzWeb Developer(ph)718.877.1325 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
Yehuda Katz schrieb: It doesn't have to do with DOM Scripting, but then, neither does $.ajax. It's a commonly enough required feature that obviously doesn't belong in the core, but would be nice to tack on to an existing jQuery installation as a plugin without worrying about incompatibilities. Here is a class based on scripts at quirksmode.org, just needs to be ported to jQuery. If nobody does it, I will make a plugin out of it: var CookieHandler = new function() { /** * Reads a cookie. * * @param name the name of the cookie * * @return the value of the cookie * * @author Klaus Hartl (28.06.2006) */ this.get = function(name) { var nameEQ = name + '='; var ca = document.cookie.split(';'); for (var i = 0; i ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); } } return null; }; /** * Sets a cookie with the given name and value and other optional parameters. * * @param namethe name of the cookie * @param value the value of the cookie * @param expires an integer specifying the expiration date from now on *in days. If you set the number of days to 0 the cookie *is trashed when the user closes the browser. * @param pathpath where the cookie is valid (default: path of *calling document). * @param domain domain where the cookie is valid (default: domain of *calling document). * @param secure boolean value indicating if the cookie transmission *requires a secure transmission. * * @author Klaus Hartl (28.06.2006) */ this.set = function(name, value, expires, path, domain, secure) { if (typeof expires == 'number') { var date = new Date(); date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toGMTString(); } else { expires = ''; } path = (path) ? '; path=' + path : ''; domain = (domain) ? '; domain=' + domain : ''; secure = (secure) ? '; secure' : ''; document.cookie = name + '=' + value + expires + path + domain + secure; }; /** * Deletes a cookie by setting the expiry date in the past. * * @param name the name of the cookie * * @author Klaus Hartl (28.06.2006) */ this.erase = function(name) { this.set(name, '', -1); }; }; ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
I never really worked with cookies, therefore I'm not sure if those methods would have any practical use at all.Cookies are limited to 4KB of data I believe, so they might be useful for short pieces of text like user preferences and such. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
Under RFC 2695, a user-agent (such as a Web browser) should provide at least 4096 bytes per cookie - so the 4KB is a minimum for storage - not a maximum. See: http://www.faqs.org/rfcs/rfc2965 (section 5.3). Yes, but SHOULD is not MUST. http://rfc.net/rfc2119.html All modern browsers do respect the 4KB number though, which is a decent-sized chunk of data for everything but texboxes. I can think of situations where It would be useful to persist a form's state into a cookie and restore it later. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
I think it would be best if we kept this in the typically succinct syntax of jQuery. such as .html() setting and getting depending on the attributes. This is a simple version that just consolidates your functions but... $.cookie = function(name, value, expires, path, domain, secure) { if (typeof value == 'undefined') { var name += =; var ca = document.cookie.split(';'); for (var i = 0; i cs.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); } } return null; } else if (value === false) { arguments.callee(name,'',-1); } else { if (typeof expires == 'number') { var date = new Date(); date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toGMTString(); } else { expires = ''; } path = (path) ? '; path=' + path : ''; domain = (domain) ? '; domain=' + domain : ''; secure = (secure) ? '; secure' : ''; document.cookie = name + '=' + value + expires + path + domain + secure; } }; Klaus Hartl wrote: Yehuda Katz schrieb: It doesn't have to do with DOM Scripting, but then, neither does $.ajax. It's a commonly enough required feature that obviously doesn't belong in the core, but would be nice to tack on to an existing jQuery installation as a plugin without worrying about incompatibilities. Here is a class based on scripts at quirksmode.org, just needs to be ported to jQuery. If nobody does it, I will make a plugin out of it: var CookieHandler = new function() { /** * Reads a cookie. * * @param name the name of the cookie * * @return the value of the cookie * * @author Klaus Hartl (28.06.2006) */ this.get = function(name) { var nameEQ = name + '='; var ca = document.cookie.split(';'); for (var i = 0; i ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); } } return null; }; /** * Sets a cookie with the given name and value and other optional parameters. * * @param namethe name of the cookie * @param value the value of the cookie * @param expires an integer specifying the expiration date from now on *in days. If you set the number of days to 0 the cookie *is trashed when the user closes the browser. * @param pathpath where the cookie is valid (default: path of *calling document). * @param domain domain where the cookie is valid (default: domain of *calling document). * @param secure boolean value indicating if the cookie transmission *requires a secure transmission. * * @author Klaus Hartl (28.06.2006) */ this.set = function(name, value, expires, path, domain, secure) { if (typeof expires == 'number') { var date = new Date(); date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toGMTString(); } else { expires = ''; } path = (path) ? '; path=' + path : ''; domain = (domain) ? '; domain=' + domain : ''; secure = (secure) ? '; secure' : ''; document.cookie = name + '=' + value + expires + path + domain + secure; }; /** * Deletes a cookie by setting the expiry date in the past. * * @param name the name of the cookie * * @author Klaus Hartl (28.06.2006) */ this.erase = function(name) { this.set(name, '', -1); }; }; ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
Under RFC 2695, a user-agent (such as a Web browser) should provide "at least 4096 bytes per cookie" – so the 4KB is a minimum for storage – not a maximum. See: http://www.faqs.org/rfcs/rfc2965 (section 5.3).Well, that's news to me. Thanks for the clarification. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
But remember that it is very easy for the user to turn off or suppress cookies. My numbers are showing around 1% without cookies. (~.1% have no javascript, but I think we are skewed a bit on that). I would rely on javascript before relying on a cookie. But that is just me -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dave Methvin Sent: Wednesday, September 13, 2006 2:22 PM To: 'jQuery Discussion.' Subject: Re: [jQuery] Cookie handling in JQuery Under RFC 2695, a user-agent (such as a Web browser) should provide at least 4096 bytes per cookie - so the 4KB is a minimum for storage - not a maximum. See: http://www.faqs.org/rfcs/rfc2965 (section 5.3). Yes, but SHOULD is not MUST. http://rfc.net/rfc2119.html All modern browsers do respect the 4KB number though, which is a decent-sized chunk of data for everything but texboxes. I can think of situations where It would be useful to persist a form's state into a cookie and restore it later. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
What would you like to see in a plugin? Rey... Yehuda Katz wrote: Would def. make a nice plugin. On 9/11/06, *Rey Bango* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Does JQuery have any methods specific to working with cookies (eg: set, get, et al) or should I just one of the many premade functions out in the wild? Rey... ___ jQuery mailing list discuss@jquery.com mailto:discuss@jquery.com http://jquery.com/discuss/ -- Yehuda Katz Web Developer (ph) 718.877.1325 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Cookie handling in JQuery
Would def. make a nice plugin.On 9/11/06, Rey Bango [EMAIL PROTECTED] wrote: Does JQuery have any methods specific to working with cookies (eg: set,get, et al) or should I just one of the many premade functions out inthe wild?Rey...___ jQuery mailing listdiscuss@jquery.comhttp://jquery.com/discuss/-- Yehuda Katz Web Developer(ph)718.877.1325 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/