标签:设置 etc lse item cookie day regexp get rem
localStorage
window.localStorage.setItem(‘a‘, 1) (window. 可省略) localStorage.setItem(‘a‘, 1) //存储数据 localStorage.getItem(‘a‘) //获取数据 localStorage.removeItem(‘a‘) //删除指定数据 localStorage.clear( ) //删除全部数据
cookie
document.cookie = ‘name=1‘; //设置cookie console.log(document.cookie); //获取cookie document.cookie = ‘name=1;expires=‘ + new Date(new Date().setTime(new Date().getTime()-1)).toGMTString(); //通过过期时间删除cookie
//JS操作cookies方法! //写cookies function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } //读取cookies function getCookie(name) { var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; } //删除cookies function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); }
标签:设置 etc lse item cookie day regexp get rem
原文地址:https://www.cnblogs.com/suntongxue/p/13328986.html