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/' });
Did you try $.cookie("name", null);
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
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
or when removing cookies do something like this:
If you use a domain parameter when creating a cookie, this will work
What works for me is setting the cookie to null before removing it:
$.cookie("filter", null); $.removeCookie("filter);
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:
This simple way it works fine: