Document.cookie not working

If you’re having trouble with “document.cookie” it’s likely you’re working locally and not on an apache server.

opening “index.html” in your default browser will fail however “localhost:8080/index.html” will work.

If you have PHP installed locally you can power up an apache server easily by running the following command

php -S localhost:8080

The above command will start a server in the location that you ran the command.

		document.addEventListener("DOMContentLoaded", function(){
			document.cookie = "name=Chris;expires=Tue, 14 Oct 2020 20:23:32 GMT; path=/"
			allCookies = document.cookie;
			console.log('Cookie' + allCookies)

Categories: Javascript