使用 jquery 删除 Cookie 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18486165/
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
remove Cookie using jquery not working
提问by Mahesh Gaikwad
I have two cookies in my JS file and I want to remove them.
我的 JS 文件中有两个 cookie,我想删除它们。
I have tried the code below but it is not working
我已经尝试了下面的代码,但它不起作用
$.removeCookie('filter', { path: '/Home/' });
$.removeCookie('Pfilter', { path: '/Home/' });
I have also tried the below for null cookies, but this is also not working.
我也尝试了下面的 null cookie,但这也不起作用。
Thanks for the help
谢谢您的帮助
$.cookie('filter',null, { path: '/Home/' });
回答by Mark
It might depend on what path your cookie is using. If you goto the chrome developer tools and check the path column under Resources > Cookies > Path.
这可能取决于您的 cookie 使用的路径。如果您转到 chrome 开发人员工具并检查资源 > Cookies > 路径下的路径列。
You might be using the generic /
for your path instead of /Home/
. Give the code below a try.
您可能正在/
为您的路径使用泛型而不是/Home/
. 试试下面的代码。
To delete a cookie with jQuery set the value to null:
要使用 jQuery 删除 cookie,请将值设置为 null:
$.removeCookie('filter', { path: '/' });
回答by Ravia
Did you try $.cookie("name", null);
你有没有试过 $.cookie("name", null);
$.removeCookie('filter', { path: '/' });
回答by jhavatar
What works for me is setting the cookie to null before removing it:
$.cookie("filter", null);
$.removeCookie("filter);
对我有用的是在删除之前将 cookie 设置为 null:
$.cookie("filter", null);
$.removeCookie("filter);
回答by rocktheartsm4l
I was having the same issue with jquery version 1.7.1 and jquery cookie version 1.4.1
我在 jquery 1.7.1 版和 jquery cookie 1.4.1 版上遇到了同样的问题
This was driving me crazy so I decided to dive into the source code and I figured out what is wrong.
这让我发疯了,所以我决定深入研究源代码,并找出问题所在。
Here is the definition of $.removeCookie
这是 $.removeCookie 的定义
$.removeCookie = function (key, options) {
if ($.cookie(key) === undefined) { // this line is the problem
return false;
}
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};
As you can see when the function checks if the cookie exists it doesn't take the options object into account. So if you are on a different path than the cookie you're trying to remove the function will fail.
正如您所看到的,当函数检查 cookie 是否存在时,它不考虑选项对象。因此,如果您在与 cookie 不同的路径上,您尝试删除该功能将失败。
A Few Solutions:
一些解决方案:
Upgrade Jquery Cookies. The most recent version doesn't even do that sanity check.
升级 Jquery Cookie。最新版本甚至不进行健全性检查。
or add this to you document ready
或将此添加到您准备好的文档中
$.removeCookie = function (key, options) {
if ($.cookie(key, options) === undefined) { // this line is the fix
return false;
}
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};
or when removing cookies do something like this:
或者在删除 cookie 时执行以下操作:
$.cookie('cookie-name', '', { path: '/my/path', expires:-1 });
回答by SilverSurfer
This simple way it works fine:
这种简单的方法可以正常工作:
$.cookie("cookieName", "", { expires: -1 });
回答by u?ur yetkil
If you use a domain parameter when creating a cookie, this will work
如果您在创建 cookie 时使用域参数,这将起作用
$.removeCookie('cookie', { path: '/' , domain : 'yourdomain.com'});