js添加获取删除cookie

//添加cookie
function setCookie(name,value,second){
  if(!name||!value) return;
    var exp = new Date();
    if(!second){
     exp.setDate(exp.getDate()+1);
     exp.setHours(0);
     exp.setMinutes(0);
     exp.setSeconds(0);
    }else{
    exp.setTime(exp.getTime() + second * 1000);
   }
  document.cookie = name + "="+ encodeURIComponent(value) +";expires="+ exp+";path=/";
}
//获取cookie
function getCookie(name){
   var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
   if(arr != null) return decodeURIComponent(arr[2]);
    return null;
   }
//删除cookie
function delCookie(name){
   var cval=getCookie(name);
   if(cval){
    var exp = new Date();
    exp.setDate(exp.getDate() - 1);
    document.cookie= name +"="+encodeURIComponent(cval)+";expires="+exp;
   }
}
 

 

posted @ 2021-07-25 22:05  90丶  阅读(103)  评论(0编辑  收藏  举报