最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - deleting cookie from parent domain - Stack Overflow

programmeradmin4浏览0评论

I am in "foo.bar", I am setting the cookie with key "abc" for the domain ".bar"

So far so good.

Now, I am still in "foo.bar" but I want to delete the cookie with key "abc" from domain ".bar"

How do I do that? I am doing JavaScript.

I am in "foo.bar.", I am setting the cookie with key "abc" for the domain ".bar."

So far so good.

Now, I am still in "foo.bar." but I want to delete the cookie with key "abc" from domain ".bar."

How do I do that? I am doing JavaScript.

Share Improve this question asked Dec 26, 2012 at 23:18 hrishikeshp19hrishikeshp19 9,03627 gold badges82 silver badges144 bronze badges 2
  • 2 As far as I know, this is only possible in PHP. I may be wrong. Also, deleting a cookie is just setting it to an expiration date before the current time. – SeinopSys Commented Dec 26, 2012 at 23:26
  • 3 How did you set the cookie? – mutil Commented Dec 26, 2012 at 23:29
Add a ment  | 

3 Answers 3

Reset to default 4

You need to explicitly provide the domain

document.cookie = 'abc=' + ";expires=Thu, 01 Jan 1970 00:00:01 GMT ;domain=.foo.;path=/";

If you use jquery.cookie, you specify the domain and path:

$.removeCookie('cookie_name', {domain: '.food', path:'/'});

While counter-intuitive, I've found the behavior seen by OP to be expected in both Chrome and Firefox. It's possible to create a cookie at a parent domain from a subdomain, but not possible to delete it afterward. This answer to another post describes this in a little more detail.

发布评论

评论列表(0)

  1. 暂无评论