Why would setting [removed] not work in Chrome?

前端 未结 4 1047
逝去的感伤
逝去的感伤 2020-12-06 10:29

My coworker ran into an issue where NO cookie could be set on Chrome via code like this:

document.cookie = \"TEST=1; expires=Tue, 14 Oct 2014 20:23:32 GMT; pat

4条回答
  •  萌比男神i
    2020-12-06 10:55

    The way cookies work, at least in Chrome, is a bit weird.

    If you need to change a cookie's value, then you need to add/set each keys one by one.

    Try this in your console:

    document.cookie; // -> "expires=Tue, 14 Oct 2014 20:23:32 GMT; path=/"
    document.cookie = 'TEST=1';
    document.cookie; // -> "TEST=1; expires=Tue, 14 Oct 2014 20:23:32 GMT; path=/"
    

    Yes, it has added the key, and not replace the whole cookie with TEST=1.

    If you need to remove a key, you can simple provide no value: TEST=.

    I hope this will get you out of the cookie nightmare (it was for me).

提交回复
热议问题