html页面怎么删除cookie,javascript中怎么删除cookie?
当web服务器向浏览器发送网页后,连接被关闭,服务器会忘记用户的一切。Cookie是为了解决“如何记住用户信息”而发明的。有时为了我们的信息安全需要删除Cookie,我们该如何做呢?

什么是 cookie?
Cookie 是在您的计算机上存储在小的文本文件中的数据。
当 web 服务器向浏览器发送网页后,连接被关闭,服务器会忘记用户的一切。
Cookie 是为了解决“如何记住用户信息”而发明的:
● 当用户访问网页时,他的名字可以存储在 cookie 中。
● 下次用户访问该页面时,cookie 会“记住”他的名字。
Cookie 保存在名称值对中,如:username = Bill Gates
当浏览器从服务器请求一个网页时,将属于该页的 cookie 添加到该请求中。这样服务器就获得了必要的数据来“记住”用户的信息。
JavaScript如何删除cookie?
首先要找到该Cookie对应的Name对应的值,然后使用expire属性设置为过期日期(即任何过去日期)就可删除cookie。
expire属性将cookie的状态保持到指定的日期和时间内,即声明了表示cookie的活动期间的日期和时间。一旦声明的时间过去,就会自动删除cookie。。document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
您应该定义 cookie 路径以确保删除正确的 cookie。
如果你不指定路径,一些浏览器不会让你删除 cookie。
找到Cookie有下面方法:function getCookie2(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]); return null;
}
找到后设置为过期,切记设置domain和path,只有这两个参数跟你要删除的参数完全一样才能把它删除掉。function resetNfluent(){
alert("before=>"+document.cookie);
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie2('name');
var lanObj=document.getElementById('lanOption');
var lanSel=lanObj.value;
alert(lanSel);
if(lanSel=='en'){
alert('let\'s reset nFluent');
alert('cval=>'+cval);
if(cval!=null){
document.cookie="name="+cval+";domain=.example.com;expires="+exp.toGMTString()+";path=/";
}else{
document.cookie="name=;domain=.example.com;expires="+exp.toGMTString()+";path=/";
}
}else{
alert('Don\'t need reset nFluent');
}
alert("after=>"+document.cookie);
}
更多推荐
所有评论(0)