I want to use jQuery to delete cookies; I have tried this:
$.cookie('name', '', { expires: -1 });
But when I refresh the page, the cookie is still there:
alert('name:' +$.cookie('name'));
Why?
I want to use jQuery to delete cookies; I have tried this:
$.cookie('name', '', { expires: -1 });
But when I refresh the page, the cookie is still there:
alert('name:' +$.cookie('name'));
Why?
To delete a cookie with JQuery, set the value to 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.
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: '/' });
if (value === null) { value = '';options.expires = -1;}
, that what goes inside the processing function, so they are supposed to perform the same. (parameters are (name, value, options)
) –
Mclaurin path
in the options to both commands, as it defaults to the path of the current page. Test by setting to root of your domain both in all places where the cookie is read and written: $.cookie('name', value, {path:'/'})
–
Troika $.removeCookie('cookie_name')
does. –
Stralsund You can try this:
$.removeCookie('the_cookie', { path: '/' });
You can also delete cookies without using jquery.cookie plugin:
document.cookie = 'NAMEOFYOURCOOKIE' + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
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.
Try this
$.cookie('_cookieName', null, { path: '/' });
The { path: '/' } do the job for you
Worked for me only when path
was set, i.e.:
$.cookie('name', null, {path:'/'})
Actually the second part of the accepted answer is OK only for some cases, it depends on the js-cookie
version that you have.
So either use js-cookie
v1.5.1 $.removeCookie
($.cookie
for other operations), or if you are using 2.0.0 or higher, the old APIs were changed to Cookies.remove
(Cookies.set
, Cookies.get
, etc.)
I found a complete answer here.
What you are doing is correct, the problem is somewhere else, e.g. the cookie is being set again somehow on refresh.
© 2022 - 2024 — McMap. All rights reserved.