SameSite cookie setup: 6 attributes and how each one works

PublishedToolWeb Cookie Generator & Parser

Contents
  1. Where people get stuck building a session cookie
  2. How to build a cookie string by hand, without a tool
  3. Common points of confusion
  4. Recap
  5. Frequently asked questions

No matter how many times you write HttpOnly into document.cookie, it never makes it into the real cookie. That isn't a mistake — it's the spec. HttpOnly can't be set from browser JavaScript at all, because it's only valid in the HTTP response header the server sends.

This article covers how the six attributes — Domain, Path, Max-Age, SameSite, Secure, and HttpOnly — actually combine inside a cookie string, and why a document.cookie assignment and a Set-Cookie header produce different results, following exactly how the code behaves.

Summary ① HttpOnly is only valid in the server's Set-Cookie header, and you can never set it through document.cookie. ② Domain and Path decide where the cookie gets sent, while Max-Age sets the expiration time in seconds. ③ When parsing a cookie string, split it on semicolons (;), then cut each segment at only the first equals sign (=) so an "=" inside a value doesn't get broken.

If you've ever built a login session cookie by hand, you've probably hit this. You type document.cookie = "session=abc123; HttpOnly" into the browser devtools console, then read document.cookie again, and the word HttpOnly is nowhere to be found.

It's not a typo, and it's not a browser bug. document.cookie only works inside the JavaScript execution context, while HttpOnly is defined as "keep JavaScript from reading or writing this cookie."

If a script could put that restriction on itself, the restriction would mean nothing. So the browser silently ignores any HttpOnly marker that comes in through document.cookie.

A similar snag happens when the cookie goes missing on cross-site requests. If you set SameSite to None for a payment widget or an external iframe and the cookie still isn't sent, the usual cause is a missing Secure attribute.

Modern browsers won't store or send a SameSite=None cookie at all if Secure isn't there too.

Once you know the rules, you can build a cookie string by hand and get it exactly right. Here's the order.

  1. Encode key=value first. Cookie strings use the semicolon (;) and the equals sign (=) as delimiters, so if a value contains either one, a space, or Korean text, you need to run it through URL encoding to keep the string intact.
  2. Join only the attributes you need with "; ". Add them as name=value pairs like Domain=example.com, Path=/, and Max-Age=3600. Attributes with no value (Secure, HttpOnly) are just the name.
  3. HttpOnly has a different destination. If you want to run the string in JavaScript, assign it as document.cookie = "...", but in that case HttpOnly is a server-header-only attribute that gets ignored even if you include it. If you want to send it as a server response, put it straight into an HTTP header as Set-Cookie: ..., and HttpOnly works as intended.

How to choose Max-Age, Domain, and Path values

You only need one of Max-Age or Expires. Max-Age takes an integer for how many seconds from now the cookie expires, and if you leave the value out, the cookie becomes a session cookie that disappears when you close the browser window. Expires requires an absolute date as a string, so you have to get the format right, which is why Max-Age is more common.

If you omit Domain, you get a host-only cookie tied exactly to the host you're on, so it isn't sent to subdomains. Set it explicitly, like Domain=example.com, and the cookie rides along on requests to example.com and to subdomains such as sub.example.com.

If you omit Path, the browser calculates a default from the path of the request that created the cookie. Set Path=/ and the cookie is sent on every path under the domain; narrow it to something like Path=/account and it's only sent on that path and its subpaths. You can check this calculation right in our free tool.

Web Cookie Generator & Parser

Enter the key/value plus Domain, Path, Max-Age, SameSite, Secure and HttpOnly, and the tool generates the document.cookie statement and the Set-Cookie header string at the same time.

Common points of confusion

SameSite and Secure move as a set. Using SameSite=None means you're willing to send the cookie on cross-site requests, which also means more chances for the cookie to be exposed. That's why browsers reject a cookie that has SameSite=None and no Secure.

It's normal for the document.cookie output line to show no trace of HttpOnly even after you tick the HttpOnly box. HttpOnly exists to keep JavaScript out, so putting it in a string that JavaScript will execute is pointless to begin with.

AttributeWhat it controlsValue formatIf omitted
DomainWhich hosts the cookie is sent toexample.comSent only to the exact host you're on (subdomains excluded)
PathWhich URL paths the cookie is sent to/accountLimited based on the request path that created the cookie
Max-AgeHow many seconds until it expires3600 (in seconds)Session cookie (deleted when the browser closes)
SameSiteWhether to send it on requests from other sitesStrict / Lax / NoneVaries by browser, but most modern browsers treat it as Lax
SecureWhether to send it only over HTTPSName only, no valueSent over HTTP too
HttpOnlyWhether JavaScript can read the cookieName only, no value; header onlyReadable through document.cookie

Values copied from browser devtools sometimes come with a Cookie: prefix. Strip that prefix before parsing, or the first key gets misread as Cookie itself.

When you split each segment on the equals sign (=), cut at only the first one. Some token values use equals signs as padding, and splitting at every equals sign chops the value off in the middle.

Recap

  • Domain and Path set where the cookie gets sent; Max-Age sets when it expires, in seconds.
  • To use SameSite=None, turn on Secure too, or the browser will reject the cookie.
  • HttpOnly is only valid in the server's Set-Cookie header. You can't set it or clear it through document.cookie.
  • When parsing a cookie string, drop the Cookie: prefix first, split on semicolons, then cut each segment at only the first equals sign so values stay intact.
Web Cookie Generator & Parser

Enter the key/value plus Domain, Path, Max-Age, SameSite, Secure and HttpOnly, and the tool generates the document.cookie statement and the Set-Cookie header string at the same time.

Frequently asked questions

No. HttpOnly is defined as the attribute that blocks JavaScript access, so the cookie won't appear in document.cookie and overwriting its value won't take effect. Only the server can set or change an HttpOnly cookie, through the Set-Cookie header in an HTTP response.

Check whether the Secure attribute is there too. Modern browsers won't store or send a SameSite=None cookie if Secure is missing. Turning on Secure in an HTTPS environment fixes it.

What's the difference between Max-Age and Expires?

Max-Age is relative: you write an integer for how many seconds from now the cookie expires. Expires is absolute: you write the expiration time as a date string. If you set neither, the cookie becomes a session cookie and is deleted when you close the browser.

If you put them in as-is, they can collide with delimiters like the semicolon (;), the equals sign (=), and spaces, breaking the string. It's safer to run the value through URL encoding, put the safe characters into the cookie string, and decode it again when you read the value back.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. async vs defer: how script loading and execution differSee how default, async, and defer scripts change HTML parsing, download, and execution on a timeline, and why parser blocking affects Core Web Vitals.
  2. CSS scrollbar color: why it works in Chrome but not FirefoxWhy scrollbar color CSS works in Chrome but not Firefox: the ::-webkit-scrollbar vs scrollbar-color difference, plus how to generate both codes at once.
  3. Share Secret Text Without Storing It on a ServerHow to compress and encrypt text in your browser and share it in a URL hash, with no server storage — plus why password lock is what actually encrypts it.
  4. Uppercase/Lowercase Converter: Sentence, Title, Snake & Kebab CaseLearn the rules behind Sentence Case, Title Case, snake_case, and kebab-case, plus the options you can use in our converter to fix English text fast.
  5. URL Slug Generator Rules: Lowercase, Hyphens, Clean SlugsSee the exact rules and regex that turn a title into a clean lowercase hyphen slug, and compare results with and without Korean characters kept.
  6. Smart Quotes Cleaner: Convert Curly Quotes to ASCIIWord and messenger editors turn straight quotes into curly ones, which breaks code. Here's why it happens and how to convert them back to plain ASCII.

Related tools