Max Max - 6 months ago 9
Javascript Question

Is it safe to set cookie with unquoted path?

I would like to execute the following JavaScript to set a browser cookie:

document.cookie = "name=value;path='/'"


This works fine in Firefox, Chrome, and Safari. It does not work in IE, however. Removing the path part or unquoting
'/'
seems to set the cookie correctly in IE. I'm not an expert on the cookie spec. All of the guides online seem to quote the path. Is it required or optional to quote the path?

Answer

According to W3Schools correct syntax is following:

With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie belongs to the current page.

document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

http://www.w3schools.com/js/js_cookies.asp