Jquery, delete cookies

I want to use jQuery to delete cookies; I tried this

$.cookie('name', '', { expires: -1 }); 

Then I refresh the page and the cookie still exists:

 alert('name:' +$.cookie('name')); 

Why? Thanks

+73
jquery cookies jquery cookie
Sep 08 '10 at 20:35
source share
7 answers

To delete a cookie using jQuery, set to null:

 $.cookie("name", null, { path: '/' }); 

Edit: The final solution was to explicitly specify the path property when accessing the cookie, since the OP accesses the cookie from several pages in different directories, and thus the default paths were different (this was not described in the original question). The decision was opened in the discussion below, which explains why this answer was accepted, despite the fact that it was not correct.

For some versions of jQ cookie, the solution above will set the cookie to null. Thus, the cookie is not deleted. Instead, use the code as suggested below.

 $.removeCookie('the_cookie', { path: '/' }); 
+115
Sep 08 '10 at 20:51
source share
— -

You can try the following:

 $.removeCookie('the_cookie', { path: '/' }); 

source: https://github.com/carhartl/jquery-cookie#readme

+62
Jun 05 '13 at 11:32
source share

You can also delete cookies without using the jquery.cookie plugin:

 document.cookie = 'NAMEOFYOURCOOKIE' + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; 
+11
May 21 '13 at 16:30
source share

this is a cookie misunderstanding problem. Browsers recognize cookie values ​​not only for keys, but also for comparing path and domain. Thus, browsers recognize a different value, the cookie value of which is the "name" with the server settings (path = '/'; domain = 'mydomain.com'), and the key is the name with no option.

+5
03 Oct '11 at 10:01
source share

try it

  $.cookie('_cookieName', null, { path: '/' }); 

{path: '/'} does the job for you

+2
Apr 6 '13 at 1:33
source share

Worked for me only when path was installed, i.e.:

 $.cookie('name', null, {path:'/'}) 
0
Apr 30 '14 at 12:09
source share

What you do is right, the problem is elsewhere, for example. The cookie is set again somehow during the update.

-one
Sep 08 2018-10-18T00:
source share



All Articles