[limb-svn] r5931 - 3.x/trunk/limb/js/shared/js

svn at limb-project.com svn at limb-project.com
Mon Jun 4 13:30:08 MSD 2007


Author: pachanga
Date: 2007-06-04 13:30:08 +0400 (Mon, 04 Jun 2007)
New Revision: 5931
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5931

Modified:
   3.x/trunk/limb/js/shared/js/limb.js
Log:
-- Limb.cookie(name, value, options) added(ported from jQuery cookie plugin)
-- Limb.trim added, String.prototype.trim uses it

Modified: 3.x/trunk/limb/js/shared/js/limb.js
===================================================================
--- 3.x/trunk/limb/js/shared/js/limb.js	2007-06-03 16:10:39 UTC (rev 5930)
+++ 3.x/trunk/limb/js/shared/js/limb.js	2007-06-04 09:30:08 UTC (rev 5931)
@@ -11,13 +11,15 @@
 
 if(Limb == undefined) var Limb = {};
 
+Limb.trim = function(str)
+{
+  var r = /^\s+|\s+$/;
+  return str.replace(r,'');
+}
+
 if(!String.prototype.trim)
 {
-  String.prototype.trim = function()
-  {
-    var r=/^\s+|\s+$/;
-    return this.replace(r,'');
-  }
+  String.prototype.trim = Limb.trim;
 }
 
 if(!Function.prototype.bind)
@@ -151,6 +153,67 @@
   return typeof(variable) == 'function';
 }
 
+/**
+ * Create a cookie with the given name and value and other optional parameters.
+ *
+ * @example Limb.cookie('the_cookie', 'the_value');
+ * @desc Set the value of a cookie.
+ * @example Limb.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
+ * @desc Create a cookie with all available options.
+ * @example Limb.cookie('the_cookie', 'the_value');
+ * @desc Create a session cookie.
+ * @example Limb.cookie('the_cookie', null);
+ * @desc Delete a cookie by passing null as value.
+ */
+Limb.cookie = function(name, value, options)
+{
+  if(typeof value != 'undefined') // name and value given, set cookie
+  {
+    options = options || {};
+    if(value === null)
+    {
+      value = '';
+      options.expires = -1;
+    }
+    var expires = '';
+    if(options.expires && (typeof options.expires == 'number' || options.expires.toUTCString))
+    {
+        var date;
+        if(typeof options.expires == 'number')
+        {
+          date = new Date();
+          date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
+        }
+        else
+          date = options.expires;
+        expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
+    }
+    var path = options.path ? '; path=' + options.path : '';
+    var domain = options.domain ? '; domain=' + options.domain : '';
+    var secure = options.secure ? '; secure' : '';
+    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
+  }
+  else
+  { // only name given, get cookie
+    var cookieValue = null;
+    if(document.cookie && document.cookie != '')
+    {
+      var cookies = document.cookie.split(';');
+      for(var i = 0; i < cookies.length; i++)
+      {
+        var cookie = Limb.trim(cookies[i]);
+        // Does this cookie string begin with the name we want?
+        if(cookie.substring(0, name.length + 1) == (name + '='))
+        {
+          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+          break;
+        }
+      }
+    }
+    return cookieValue;
+  }
+}
+
 Limb.namespace('Limb.Classkit');
 
 Limb.Classkit.createClass = function(body)



More information about the limb-svn mailing list