简单的cookie读写封装

时间:2021-01-10 09:05:14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简单的cookie封装</title>
</head>
<body>
</body>
<script>
var cookies={
name:this.name,
value:this.value,
day:this.day,
setCookie:function(name,value,day){
  var oDate=new Date;
       oDate.setDate(oDate.getDate()+day);
       document.cookie=name+'='+value+';expires=+'+oDate; 
},
getCookie:function(name){
      var arr=document.cookie.split('; ');
      for(var i=0;i<arr.length;i++){
       var arr2=arr[i].split("=");
      if(arr2[0]==name){
      return arr2[1];
        }         
      }       
      return '';
}
removeCookie:function(name){
setCookie(name,1,-1);
}
}
cookies.setCookie('sex','男',10)
console.log(cookies.getCookie('sex'));
</script>
</html>