jquery,删除cookie [英] jquery, delete cookies

查看:156
本文介绍了jquery,删除cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JQuery删除Cookie;我试过这个

I want to use JQuery to delete cookies ; I tried this

$.cookie('name', '', { expires: -1 });

然后刷新页面,cookie仍然存在:

Then I refresh the page and the cookie is still there :

alert('name:' +$.cookie('name'));

为什么?感谢

推荐答案

要使用JQuery删除Cookie,请将值设置为null:

To delete a cookie with JQuery, set the value to null:

$.cookie("name", null, { path: '/' });

编辑:最终解决方案是明确指定 path 属性,因为OP访问来自不同目录中多个页面的cookie,因此默认路径不同(这在原始问题中没有描述)。解决方案在下面的讨论中被发现,这解释了为什么这个回答被接受 - 尽管不正确。

The final solution was to explicitly specify the path property whenever accessing the cookie, because the OP accesses the cookie from multiple pages in different directories, and thus the default paths were different (this was not described in the original question). The solution was discovered in discussion below, which explains why this answer was accepted - despite not being correct.

对于一些版本jQ cookie上面的解决方案将cookie设置为字符串空值。因此不会删除cookie。

For some versions jQ cookie the solution above will set the cookie to string null. Thus not removing the cookie. Use the code as suggested below instead.

$.removeCookie('the_cookie', { path: '/' });

这篇关于jquery,删除cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆