get Cookieとset Cookieのパッケージ

950 ワード

function setCookie(cookieName,value,date){
  document.cookie=cookieName+"="+value+
                 ";expires="+date.toGMTString();
}
function getCookie(cookieName){
  // document.cookie     cookie 
  var cookie=document.cookie;
  // cookie   cookieName      i 
  var i=cookie.indexOf(cookieName);
  //  i  -1,   null
  if(i==-1){return null}
  else{//    
    //i+cookieName   +1,     starti 
    var starti=i+cookieName.length+1;
    // starti    cookie    ;   endi
    var endi=cookie.indexOf(";",starti);
    if(endi==-1){//  endi -1
      //  cookie starti        ,  
      return cookie.slice(starti);
    }else{//  
      //  cookie starti endi   ,  
      return cookie.slice(starti,endi);
    }
  }
}