Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.cookie not storing cookie?

Does document.cookie only work on a server? Alert doesn't seem to be outputting my cookie.

document.cookie = "d=e";
alert(document.cookie);
like image 953
DMar Avatar asked Dec 21 '22 14:12

DMar


2 Answers

Some browsers won't honor cookies when set by a document with the file protocol.

Chrome intentionally disables cookies on file://, for a variety of reasons. When we wrote our cookie support, the correct behavior was unclear when dealing with things like file shares. We found cookies on file:// to be fairly uncommon, and it has a few negative security implications.

You can, however, enable them with the command line flag --enable-file-cookies.

Cookies are strictly a HTTP mechanism as per RFC 2109. There should be no reasonable expectation for them to work for protocols other than HTTP, much less file:///, and it is not clear what their behavior should be, and what rules of security compartmentalization should apply.

Source.

like image 190
alex Avatar answered Dec 28 '22 23:12

alex


Cookies are associated with a particular domain. If you just open your HTML document as a file, then how is the browser to know what domain to associate a cookie with, or what cookies it should allow that file to access?

So yes, for all practical purposes, your browser will only allow cookies to be stored and retrieved if your HTML document is served by a server/accessed via an HTTP URL that allows it to determine some sort of domain to associate the cookie(s) with.

like image 44
aroth Avatar answered Dec 29 '22 00:12

aroth