title: Javascript set cookie for current domain including subdomains
body: <p>Is it possible to store a JS cookie for the <strong>current</strong> domain <strong>including subdomains</strong>.</p>
  
  <p>e.g.: 
  <code>document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/;secure"</code> sets a cookie for the current domain but does not add a dot to the domain name. </p>
  
  <p>I know that it is possible to specify the domain via <code>domain=.example.com</code> but I do not want to hardcode the domain name.</p>
  
  <p>I tried something like this but it did not work out: 
  <code>document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/;secure;domain=."</code></p>
  
  <p><strong>Update:</strong></p>
  
  <p>I know you can get the current domain with <code>window.location.hostname</code> but is there a solution where i do not need to get the domain name programmatically</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>like described here: <a href="https://stackoverflow.com/questions/9618217/what-does-the-dot-prefix-in-the-cookie-domain-mean">What does the dot prefix in the cookie domain mean?</a></p>
  
  <blockquote>
    <p>The leading dot means that the cookie is valid for subdomains as well; nevertheless recent HTTP specifications (RFC 6265) changed this rule so modern browsers should not care about the leading dot. The dot may be needed by old browser implementing the deprecated RFC 2109.</p>
  </blockquote>
  
  <p>This means that it <strong>does not make a difference</strong> if you use a dot before the domain name in modern browsers. That said, you can leave the domain section of the JS cookie blank and it is set to the current domain (which also matches subdomains)</p>