Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read cookies available to the current page with document.cookie. It returns a semicolon-separated string of name=value pairs, not a JavaScript object. For example, a page might receive theme=dark; session_hint=abc. JavaScript cannot read cookies marked HttpOnly.

Read the current page’s cookies

The cookie property on document has a getter and a setter. Reading it retrieves the cookies exposed to the current document:

const cookieString = document.cookie;
console.log(cookieString);

The result is a string of cookie name/value pairs separated by semicolons. It may be empty when no cookies are available to the document. It is not JSON, an array, or a Map, and it does not show every cookie the browser stores for a site.

Find one cookie by name

To look up one cookie, split the string at semicolons, trim whitespace around each entry, and match the requested name. Split only at the first equals sign: a value may itself contain equals signs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
function readCookie(name) {
  const prefix = `${name}=`;
  const item = document.cookie
    .split(";")
    .map((part) => part.trim())
    .find((part) => part.startsWith(prefix));

  return item ? item.slice(prefix.length) : undefined;
}

const theme = readCookie("theme");
console.log(theme);

This is a small parser built around the browser’s serialized cookie format, not a built-in parsing API. It returns undefined if no matching name is present. If your application encodes cookie values when setting them, decode them only according to the format your application uses. Treat script-readable cookie values as untrusted input: users can inspect and modify them.

Why a cookie may not appear

HttpOnly cookies are intentionally hidden

A cookie set with the HttpOnly attribute cannot be read by page JavaScript, including with document.cookie. The browser can still send it to the server when its scope and sending rules allow. This is useful for session credentials that do not need to be exposed to client-side code: keeping them inaccessible to scripts reduces the opportunity for injected script to steal their values.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

If an authentication cookie is HttpOnly, do not try to retrieve it in JavaScript or expose the session secret just to attach it to a request. Let the browser send eligible cookies and configure the server and request credentials policy for the authentication flow.

Cookie scope affects availability and sending

Domain and path scope determine which requests receive a cookie, and browser rules affect when cookies are sent. The Path attribute is not a security boundary that prevents scripts on another path from reading cookies. Use HttpOnly when a cookie should not be available to scripts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Understand the security attributes

  • HttpOnly: prevents JavaScript access through document.cookie; it does not itself determine all the requests to which the browser sends the cookie.
  • Secure: restricts sending to secure HTTPS requests, subject to browser behavior for localhost. It does not by itself block JavaScript from reading a cookie.
  • SameSite: controls cookie sending in cross-site contexts. Strict, Lax, and None have different effects; SameSite=None requires Secure.
  • Path and domain: define scope for sending cookies, but path is not a substitute for protecting a value from scripts.

Choose cookie attributes on the server with the cookie’s purpose in mind. A client-side preference may need to be script-readable; a session credential usually should not be.

Reading is different from setting

The same property is used to request a cookie write, but assignment does not replace the full list returned by a read. It asks the browser to set an individual cookie:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
document.cookie = "theme=dark";

That statement is not a way to inspect cookies or outgoing request headers. It also does not expose an HttpOnly cookie. Set cookie values in a format your application defines, and do not rely on a client-editable cookie as proof of identity or authorization.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance and the asynchronous alternative

The document.cookie getter is synchronous and can block the main thread, particularly when cookie access crosses processes or involves I/O. For occasional reads this simple API may be adequate; code that manages cookies frequently can consider the asynchronous Cookie Store API. Check support in the browsers and execution contexts you target before depending on it, because availability can vary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Troubleshooting

  • The string is empty: the current document may have no cookies available to it. Check that the page is on the expected site and that the cookie’s scope and browser sending rules fit the current context.
  • A known session cookie is missing: it may be marked HttpOnly. That is expected; use the authenticated request flow rather than attempting to read the secret in JavaScript.
  • Lookup fails despite seeing the cookie string: check the exact cookie name and trim whitespace around semicolon-separated entries. The example parser does both.
  • A parsed value looks truncated: do not split an entry on every equals sign. Match the name prefix and take the remainder as the value, as the example does.
  • Cookie access causes a pause: avoid repeatedly reading the synchronous property in hot code paths. Consider the asynchronous Cookie Store API if it is supported in the environments you need.

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server, not a way to read cookies from JavaScript. For capturing a page as an image, its one-call API can return a screenshot:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for options. It accepts cookie and header parameters when you need to configure a capture, but this does not expose browser-only or HttpOnly cookie values. Before a capture, it accepts consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers say which page verdict applied and whether the request was billed. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Frequently asked questions

Does document.cookie return a JSON object?

No. It returns a serialized string of semicolon-separated name/value pairs. Parse the string if your code needs to look up a particular cookie.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can I use it to read cookies from another website?

No. It reads cookies available to the current document, not arbitrary cookies belonging to other sites.

Can JavaScript read the cookies sent with a request?

document.cookie is not an outgoing-request-header inspector. It exposes cookies available to the document; cookies marked HttpOnly remain hidden from scripts.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.