K
K
Kirill Gladikov2017-12-13 15:28:01
JavaScript
Kirill Gladikov, 2017-12-13 15:28:01

How to set cookies for a subdomain from the main domain?

Hello.
The learn.javascript.ru site says that you can specify either the main domain or its subdomain in the "domain" parameter, but I can't create a cookie for one.example.com from the example.com site. About a point before domains I know. Interested in creating cookies for one subdomain while on the main domain.
Code Example

var date = new Date(new Date().getTime() + 60 * 1000);
document.cookie = "name=value; domain=learn.javascript.ru; path=/; expires=" + date.toUTCString();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Gladikov, 2017-12-16
@Fapalz

You cannot set a cookie for one subdomain from the main domain. You can only create a common cookie for all subdomains and the main domain by specifying the site domain in the domain parameter.
More detailed answer here

V
Viktor Yanyshev, 2017-12-13
@villiwalla

path=one.domain.com

P
Pavel Kornilov, 2017-12-13
@KorniloFF

I recommend a good mini-library for working with cookies .
When working with it, it is solved like this:

cookies({ cookieKey: 'value' }, { domain: 'sub.example.com' });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question