jquery,删除cookies
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3671659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery, delete cookies
提问by user319854
I want to use JQuery to delete cookies ; I tried this
我想使用 JQuery 删除 cookie ;我试过这个
$.cookie('name', '', { expires: -1 });
Then I refresh the page and the cookie is still there :
然后我刷新页面,cookie 仍然存在:
alert('name:' +$.cookie('name'));
Why? Thanks
为什么?谢谢
回答by Chadwick
To delete a cookie with JQuery, set the value to null:
要使用 JQuery 删除 cookie,请将值设置为 null:
$.cookie("name", null, { path: '/' });
Edit: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.
编辑:最终的解决方案是path
在访问 cookie 时明确指定属性,因为 OP 从不同目录中的多个页面访问 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.
对于某些版本的 jQ cookie,上面的解决方案会将 cookie 设置为字符串 null。因此不会删除cookie。请改用下面建议的代码。
$.removeCookie('the_cookie', { path: '/' });
回答by Gert-Jan Rebel
You can try this:
你可以试试这个:
$.removeCookie('the_cookie', { path: '/' });
回答by Jan Richter
You can also delete cookies without using jquery.cookie plugin:
你也可以不使用 jquery.cookie 插件来删除 cookie:
document.cookie = 'NAMEOFYOURCOOKIE' + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
回答by logan kim
it is the problem of misunderstand of cookie. Browsers recognize cookie values for not just keys also compare the options path & domain. So Browsers recognize different value which cookie values that key is 'name' with server setting option(path='/'; domain='mydomain.com') and key is 'name' with no option.
是对cookie的误解问题。浏览器不仅会识别键的 cookie 值,还会比较选项路径和域。因此,浏览器识别不同的值,该 cookie 值的 cookie 值是带有服务器设置选项(path='/'; domain='mydomain.com')和键是 'name' 的 'name' 没有选项。
回答by Otto Kanellis
Try this
尝试这个
$.cookie('_cookieName', null, { path: '/' });
The { path: '/' } do the job for you
{ path: '/' } 为您完成工作
回答by Andron
Worked for me onlywhen path
was set, i.e.:
仅在path
设置时为我工作,即:
$.cookie('name', null, {path:'/'})
回答by aularon
What you are doing is correct, the problem is somewhere else, e.g. the cookie is being set again somehow on refresh.
您在做什么是正确的,问题出在其他地方,例如在刷新时以某种方式再次设置了 cookie。