bradrice
8/23/2011 - 7:49 PM

Set, get and delete a cookie

Set, get and delete a cookie

Cookies = {
  set: function(key, value, minstoexpire) {
    var expires = "";
    if (minstoexpire) {
      var date = new Date();
      date.setTime(date.getTime() + (minstoexpire*60*1000));
      expires = "; expires=" + date.toGMTString();
    }
    document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + expires+"; path=/";
    return value;
    },
    get: function(key) {
      var nameCompare = key + "=";
      var cookieArr = document.cookie.split(';');
      for(var i = 0; i < cookieArr.length; i++) {
        var aCrumb = cookieArr[i].split('=');
        var currentKey = decodeURIComponent(aCrumb[0]);
        if(key == currentKey || " " + key == currentKey)
          return decodeURIComponent(aCrumb[1]);
        }
      return null;
    },
    del: function(key) {
      this.set(key, "", -1);
    }
}