How do I delete a cookie from a specific domain using Javascript?

前端 未结 3 939
天涯浪人
天涯浪人 2020-12-30 06:31

Let\'s say I am at http://www.example.com and I want to delete a cookie whose domain is .example.com and another one whose domain is www.example.com

3条回答
  •  盖世英雄少女心
    2020-12-30 07:28

    You could do this only if you were at http://example.com and wanted to delete http://blah.example.com cookie. It wouldn't work from www.example.com either - only the "base" domain can delete subdomain cookies.

    There are also "all-subdomain" cookies, which start with a ., and can also only be deleted by the base domain.

    From the base domain, this should work to delete it:

    document.cookie = 'my_cookie=; path=/; domain=.example.com; expires=' + new Date(0).toUTCString();
    

    Or using the excellent jquery.cookie plugin:

    $.cookie('my_cookie',null, {domain:'.example.com'})
    

提交回复
热议问题