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 name    the 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 path    path 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/

Reply via email to