I think knowing the answer to this would help me conceptualize the relationship between the cookies stored by the browser and the document.cookie made available via the DOM.
I think knowing the answer to this would help me conceptualize the relationship between
Share
document.cookiedoesn’t really behave normally. Browsers treat calls to reading and writingdocument.cookiedifferent from most calls to object properties.Setting
document.cookiedoesn’t set the entire cookie string. Instead, it adds cookies. For example:Though the order of the cookies may vary, the snippet still illustrates the point. Setting
document.cookiesets the cookies specified, but doesn’t remove a cookie just because it’s not mentioned in the new string. It’d be too easy to make mistakes.Of course, I’m not totally sure why the API was built this way. I suspect things might be different if we were writing the cookie API today, and would actually have read, write, delete, etc., functions. However, this is what we’ve got.