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;
   }
}
 

 

js添加获取删除cookie

上一篇:前端HTML+CSS查漏补缺——2021-7月24


下一篇:社交APP的痛点及九大流派解析,微信陌陌,咱还约吗