﻿

function ClientCookie() 
  {this.key="";
   this.value="";
   this.expires=0;
     this.init=function()
                 {this.key=""; 
                this.value=""; 
                this.expires=0; 
                              }; 
   this.set=function(key,value,expires)
              {if(this.key=="")this.key=key; 
                             if(this.value=="")this.value=value; 
                             if(this.expires<=0)this.expires=expires; 
                             if(this.key==""||typeof(this.key)!="string") 
                 {alert("请先设置欲保存的cookie名称！"); 
                                  this.init(); 
                  return false; 
                 }; 
               if(this.key.match(/[,; ]/)) 
                 {alert("cookie名称中不能包含“,”、“;”或空格！"); 
                                  this.init(); 
                  return false; 
                 }; 
               if(this.value.toString().match(/[,; ]/)||typeof(this.value)=="undefined") 
                 {alert("cookie值中不能包含“,”、“;”或空格！"); 
                                  this.init(); 
                  return false; 
                 }; 
               if(this.expires<=0||typeof(this.expires)!="number") 
                 {alert("请先正确设置cookie的有效时间！"); 
                                  this.init(); 
                  return false; 
                 }; 
               var cookie=document.cookie; 
               var dt=new Date(); 
               dt.setTime(dt.getTime()+this.expires); 
                             document.cookie=this.key+"="+this.value+";path=/;expires="+dt.toGMTString(); 
                             this.init(); 
               return true; 
              }; 
   this.get=function(key)
              {if(key==""||key.match(/[,; ]/)) 
                 {alert("请正确设置欲查找的cookie名称！") 
                  return false; 
                 }; 
               var cookie=document.cookie; 
               var start=cookie.indexOf(key+"="); 
               if(start==-1) 
                 {

                  return false; 
                 }; 
               var end=cookie.indexOf(";",start); 
                             if(end==-1) 
                               end=cookie.length; 
               var getCookie=cookie.substring(start+key.length+1,end); 
               return getCookie; 
              }; 
   this.showAll=function(){alert("共有以下cookie对：\n"+document.cookie.split(";").toString().replace(/,/g,"\n"));};
   this.del=function(key)
              {
                if(key==""||key.match(/[,; ]/)) 
                 {
                  return false; 
                 }; 
                var dt=new Date(); 
                dt.setTime(dt.getTime()-1); 
                document.cookie=key+"=ClientDelete;path=/;expires="+dt.toGMTString(); 
                this.init(); 
                return true; 
              }; 
              this.destroy=function()
              {
                var dt=new Date(); 
                dt.setTime(dt.getTime()-1); 
                while(document.cookie!="") 
                document.cookie=document.cookie+";path=/;expires="+dt.toGMTString(); 
                this.init(); 
                return true;
              }; 
  };