How to delete a cookie using jQuery?

后端 未结 7 2158
终归单人心
终归单人心 2020-11-27 15:27

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

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

But when I refresh the page, the cookie

相关标签:
7条回答
  • 2020-11-27 15:33

    What you are doing is correct, the problem is somewhere else, e.g. the cookie is being set again somehow on refresh.

    0 讨论(0)
  • 2020-11-27 15:35

    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.

    0 讨论(0)
  • 2020-11-27 15:38

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

    document.cookie = 'NAMEOFYOURCOOKIE' + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    
    0 讨论(0)
  • 2020-11-27 15:39

    You can try this:

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

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

    0 讨论(0)
  • 2020-11-27 15:42

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

    $.cookie('name', null, {path:'/'})
    
    0 讨论(0)
  • 2020-11-27 15:43

    Try this

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

    The { path: '/' } do the job for you

    0 讨论(0)
提交回复
热议问题