如何用js修改页面cookies
展开全部
直接赋值。不设置时间的话,默认浏览器关闭cookie失效
document.cookie = "name="+value;
写个函数动态调用
//设置或添加cookie
function setCookie(name,value,time){ var str = name + "=" + escape(value); if(time > 0){ var date = new Date(); var ms = time*3600*1000; date.setTime(date.getTime() + ms); str += "; expires=" + date.toGMTString(); } document.cookie = str;}//获取cookie function getCookie(name){ //cookie中的数据都是以分号加空格区分开 var arr = document.cookie.split("; "); for(var i=0; i<arr.length; i++){ if(arr[i].split("=")[0] == name){ return arr[i].split("=")[1]; } } //未找到对应的cookie则返回空字符串 return ''; }//删除cookie function removeCookie(name){ document.cookie = name+"=;expires="+(new Date(0)).toGMTString();}
document.cookie = "name="+value;
写个函数动态调用
//设置或添加cookie
function setCookie(name,value,time){ var str = name + "=" + escape(value); if(time > 0){ var date = new Date(); var ms = time*3600*1000; date.setTime(date.getTime() + ms); str += "; expires=" + date.toGMTString(); } document.cookie = str;}//获取cookie function getCookie(name){ //cookie中的数据都是以分号加空格区分开 var arr = document.cookie.split("; "); for(var i=0; i<arr.length; i++){ if(arr[i].split("=")[0] == name){ return arr[i].split("=")[1]; } } //未找到对应的cookie则返回空字符串 return ''; }//删除cookie function removeCookie(name){ document.cookie = name+"=;expires="+(new Date(0)).toGMTString();}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询