Max
Max

Reputation: 15975

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?

Upvotes: 2

Views: 68

Answers (2)

gevorg
gevorg

Reputation: 5055

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

Upvotes: 2

Bamidele Alegbe
Bamidele Alegbe

Reputation: 534

Yes it is safe. it is the right way to do it when using plain old javascript.

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

or via Jquery plugin jquery.cookie

    $.cookie('name', 'value', { expires: 7, path: '/' });

//or

    $.cookie('name', 'value', { path: '/' });

Upvotes: 1

Related Questions