2

Recently I faced this weird issue. Cookies are supposed to be set using document.cookie = "key=value". However, when I'm setting cookies in a such way, I encounter this unexpected behaviour:

weird shit As you can see, when you assign document.cookie to anything it gets appended to the end of the cookie itself. There is simply no way to delete all cookies! It appears that instead of setting the cookie value, Chrome appends it to the end in a similar manner this would do:

var cookie;
function foo(bar){
    cookie += "; " + bar;
}

Why is this happening? As you can see, it's not a local file, I used this on StackOverflow website (logged out, of course)

user7401478
  • 1,372
  • 1
  • 8
  • 25

1 Answers1

0

Yes, what you describe is the correct way to add a cookie.

To delete a cookie, you need to set its expiration in the past:

document.cookie = key+ '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
  • I was wondering why this very strange and retarted API is this way? Was this invented before Javacript had objects? – Roel Jun 20 '18 at 12:53
  • 2
    Look at the history of JS, and what its inspirations were. It's very dubitable you could say it never had objects. More likely is that the creators of the DOM were... inconsistent, to say the least. I'd be curious to see which browser introduced cookies, as it was probably one of the competitors just tacking on features. – Kenogu Labz Aug 08 '18 at 20:53