Deleting a host-only cookie via javascript

时间:2016-08-31 18:11:07

标签: javascript cookies

Came across this particular predicament today, wanted to delete a hostOnly cookie using the good old - 'set a past date as expires' trick.

But come what may, i was not able to delete a hostOnly cookie even by setting the expiry date to 2000.

A small code snippet.

var somePast = new Date(2000,03);
document.cookie="TARGET_COOKIE=asda; path=/ ;domain=subdomain.domain.com; expires="+today

And viola, the cookie was still there.

ON searching , came across this question , and the second answer said "If you have a hostOnly cookie, do not specify the domain when you modify/expire it."

and changing the code snippet as below

   document.cookie ="TARGET_COOKIE=asda; path=/ ;expires="+today

It worked like a charm, bye bye hostOnly cookies !

  • Why does this happen?
  • i tried the same with hostOnly:false cookies ,but they werent deleted, they required a domain to be set . What is happening?

1 个答案:

答案 0 :(得分:0)

也许是因为您必须编写与创建它时完全相同的参数来删除(或编辑)它。