I have two cookies in my JS file and I want to remove them.
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.
Thanks for the help
$.cookie('filter',null, { path: '/Home/' });
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.
You might be using the generic /
for your path instead of /Home/
. Give the code below a try.
To delete a cookie with jQuery set the value to null:
$.removeCookie('filter', { path: '/' });
你有没有试过 $.cookie("name", null);
$.removeCookie('filter', { path: '/' });
What works for me is setting the cookie to null before removing it: $.cookie("filter", null); $.removeCookie("filter);
$.cookie("filter", null); $.removeCookie("filter);
I was having the same issue with jquery version 1.7.1 and jquery cookie version 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 = 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.
A Few Solutions:
Upgrade Jquery Cookies. The most recent version doesn't even do that sanity check.
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-name', '', { path: '/my/path', expires:-1 });
如果您在创建 cookie 时使用域参数,这将起作用
$.removeCookie('cookie', { path: '/' , domain : 'yourdomain.com'});
这种简单的方法可以正常工作:
$.cookie("cookieName", "", { expires: -1 });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.