JS-Cookie

1624 ワード


/**
 * cookie    
 */
CookieUtil={
   /**
    *   Cookie
    * @param {string}   cookie   
    * @param {string}   cookie  
    * @param {object}     {         }
    */   
   set   :   function(name,value,option){
      var str=name+"="+escape(value);
      if(option){
         if(option.expireDays){//    
            var date=new Date();
            var ms=option.expireDays*24*3600*1000;
            date.setTime(date.getTime()+ms);
            str+="; expires="+date.toGMTString();
         }
         if(option.path)str+="; path="+path; //      
         if(option.domain)str+="; domain"+domain; //      
         if(option.secure)str+="; true"; //     
      }
      document.cookie=str;
   },
   /**
    *   Cookie
    * @param  {string} cookie   
    * @return {string} cookie  
    */   
   get   :   function(name){
      var cookieArray=document.cookie.split("; "); //     cookie   
      for(var i=0;i<cookieArray.length;i++){
         var arr=cookieArray[i].split("="); //      
         if(arr[0]==name)
            return unescape(arr[1]); //      cookie,      
      }
      return "";
   },
   /**
    *   Cookie
    * @param  {string}      cookie  
    */   
   del : function(){
      this.set(name,"",{expireDays:-1}); //               cookie
   }
};