到期删除Cookie无效 [英] Deleting cookies by expiration not working
问题描述
我正在尝试使cookie过期,但是它不起作用:
我用
I''m trying to make cookies expire but it''s not working:
I use
function delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) )
{
document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
alert(name;);
}
else
alert("none");
}
该警报警报(名称);正确显示cookie的名称,但是它不起作用,因为我有:
That alert alert(name); show the name of the cookie correctly, however it''s not working, because I have:
var id_sala = lerCookie('idsala');
document.write(" COOKIE lida idsala: " + lerCookie('idsala')+ "<br>");
即使关闭浏览器并直接转到URL,它仍然会显示cookie ...我已经在设置cookie的位置评论了cookie(只是为了确保).我用JSP做到了:
And even if close the browser and go directly to the URL it still shows the cookie... I already commented the cookie where I set it (just to make sure). I do it with JSP:
/*Cookie cookie = new Cookie("idsala", Integer.toString(id));
cookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cookie);)*/
该删除功能与html按钮相关联:
That delete function is associated with a html button:
function logout()
{
delete_Cookie("idsala", "/", "");
( Get_Cookie( 'idsala' ) ) ? alert( Get_Cookie('idsala')) :
alert( "it is gone");
}
我不知道为什么会这样,你能帮我吗?唯一的解决方案(目前是..)是使用浏览器设置删除cookie.
I have no idea why this is happening, could you help me ? The only solution (for now..) is deleting cookies with the browser settings.
推荐答案
您不能直接删除用户计算机上的cookie.但是,您可以通过将cookie的过期日期设置为过去的日期来指示用户的浏览器删除cookie.用户下一次向设置cookie的域或路径中的页面发出请求时,浏览器将确定cookie已过期并将其删除.
You cannot directly delete a cookie on a user''s computer. However, you can direct the user''s browser to delete the cookie by setting the cookie''s expiration date to a past date. The next time a user makes a request to a page within the domain or path that set the cookie, the browser will determine that the cookie has expired and remove it.
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
http://msdn.microsoft.com/en-us/library/ms178195.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/ms178195.aspx[^]
这篇关于到期删除Cookie无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!