cookie的封装

    xiaoxiao2022-07-03  108

    <script> //封装一个获取cookie值得方法 function getCookie(key){ let str = document.cookie; let arr = str.split("; ");//分号后的空格不要删 cookie之间的键值对是通过分号+空格 for(let i=0; i<arr.length; i++){ let item = arr[i].split("="); if(item[0] == key){ return item[1]; } } //没cookie,或者有cookie,但是没找到key 得不到cookie, 返回一个空字符串 return ";" } //封装一个设置cookie值得方法 function setCookie(key,value,day){ let d = new Date(); d.setDate(d.getDate()+day); document.cookie = key+"="+value+";expires=" + d; } //删除cookie function delCookie(key){ setCookie(key," ",-1); } </script>
    最新回复(0)