Revision 296 (by ahitrov, 2013/03/26 17:59:01) Promosite (anthill) project source
new function(){
  var
  w = window,
  undef = 'undefined',
  h = 'length',
  r = 'replace';
  w.ra || (w.ra = {});
  w.ra.cookie = {
    'get': function(name) {
      var
      a = document.cookie.split(/;\s*/g),
      t = false,
      d = decodeURIComponent,
      e = /%[\da-fA-F]{2}/g,
      c = {};
      while (a[h]){
        t=a[a[h]-1].split('=');
        
        c[t[0][r](e,d)] = t.slice(1).join('');
        a[h]--
      }
      return typeof(name)!=undef ?
        ( typeof(t=c[name])!=undef ? t[r](e,d) : false ) :
        c
    },
    'set': function(name, value, opt){
      var i, t, d = encodeURIComponent,
      e = /[^<>:\^\+\$\?\&#\[\]\\,\.\{\}\'\/]+/g,
      a = [name[r](e,d),'='],
      k = 'secure,path,expires,domain'.split(','),
      f=[
        function(b, k){return (b && v) ? k : ''},
        function(b, k, v){return k + ((b && v) ? v : '/')},
        function(b, k, v){return (b && v.constructor == Date) ? k+v.toGMTString() : ''},
        function(b, k, v){return k + ((b && v )? v : location.hostname)}
      ];
      opt || (opt={});
      a[a[h]]= typeof opt['encoded'] != undef ? value : value.replace(e,d);
      
      for (i=k[h]; i--;){
        t=k[i];
        a[a[h]]=f[i](typeof opt[t] != undef, '; '+t+'=', opt[t])
      }
      if( (i=4*1024-(t=a.join('')).length) < 0 ){
        t = a[2].slice(0,i);
        i = a[2].slice(i);
        a[2]=t;
        t=a.join('')
      } else {
        i = false
      };
      document.cookie = t;
      return i
    },
    'del': function(name){
      var r = this.get(name);
      this.set(name, '', {'expires':new Date(0)});
      return r
    },
    'test': function(){
      var testName = 'test_cookie_', testValue = '{value:"%test>"}';
      this.set(testName,testValue);
      return this.del(testName) == testValue
    }
  };
  w.ra.cookie.test = w.ra.cookie.test();
//  w.ra.cookie.test = navigator.cookieEnabled;
};