1

Currently I have the cookie to be added as:

document.cookie="key=value".concat(";path=/");

And this will add a cookie with no host information. However if I changed to:

document.cookie="key=value".concat(";path=/;domain=.mydomain.ca");

The cookie will not be generated. Similarly, if I have "expires=" in the cookie it will also not be generated.

I am running on localhost, and I am not sure if there any restriction on what domain I can use? Or what else could cause failure on setting the cookie?

jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • Possible duplicate of [How to set a cookie for another domain](http://stackoverflow.com/questions/6761415/how-to-set-a-cookie-for-another-domain). The [Same Origin Policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) also [applies to `document.cookie`](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie#Notes). – Jonathan Lonowski Feb 12 '15 at 04:41
  • @JonathanLonowski The diference is that I am trying to set cookie for subdomains (have a dot before mydomain.ca), not for a total srtanger domain. I have the impression that this is possible. CORS may be the reason, I would appreciate if you can provide answer on how to set the cookie when working on localhost. – jamesdeath123 Feb 12 '15 at 04:53
  • @JonathanLonowski And if it is due to CORS - why would the expires time also disable the cookie as well? Does CORS affect the "expires" on cookie as well? – jamesdeath123 Feb 12 '15 at 05:02

1 Answers1

0

I feel like this should be in a comment, but I can't comment (need at least 50 reps) so: have you tried setting an expiration date on the cookie? After setting the cookie, can you provide us with the output of doing

window.alert(document.cookie);

I tried the code in jsFiddle and it worked: http://jsfiddle.net/8fy24pbu/

Also, your domain shouldn't be localhost because you are not running this from a http server

jkd
  • 1,045
  • 1
  • 11
  • 27