Cookie klasse i javascript

En klasse for å håndtere javascript-cookies. Kan lese, skrive, slette og sjekke om clienten støtter cookies.

// Selve cookie klassen:

var Cookies = {
    init: function(){
      var allCookies = document.cookie.split('; ');
      for (var i=0;i<allCookies.length;i++) {
      var cookiePair = allCookies[i].split('=');
      this[cookiePair[0]] = cookiePair[1];
    }
  },
    create: function(name,value,days){
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*86400000));
        var expires = "; expires="+date.toGMTString();
      }
      else var expires = ""; 
        document.cookie = name+"="+value+expires+"; path=/";
        this[name] = value;
    },
      erase: function(name){
        this.create(name,'',-1);
        this[name] = undefined;
    },
      enabled: function(){
        this.create('test','1',1);
        if (this['test']=='1'){
          this.erase('test');return true;
        }
        return false;
      }
    };
Cookies.init();

// Slik bruker du den:

//Lage cookie:
Cookie.create("cookieNavn", "innholdet", 14);

//Lese cookie
var minCookie = Cookie["cookieNavn"];

//Slette cookie
Cookier.erase("cookieNavn");