原生JS代码封装(添加cookie,获取cookie)
作者:互联网
// 添加cookie function addCookie(key,value,days) { var now = new Date(); now.setDate(now.getDate() + days); document.cookie = key+"="+value+"; expires="+now; }
// 获取cookie function getCookie(key) { var str = document.cookie; if(!str) return null; var reg1 = new RegExp(key+"=([^;]+)$"); var reg2 = new RegExp(key+"=([^;]+);"); if(reg1.test(str)) { return str.match(reg1)[1]; } else { return str.match(reg2)[1]; } }
标签:reg1,封装,key,JS,cookie,str,var,now 来源: https://www.cnblogs.com/sunyang-001/p/10812984.html