English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM cookie attribute

HTML DOM Document Object

cookieAttribute retrieval and setting related to the current document's Cookie.

Syntax:

Return cookie properties:

document.cookie

Set cookie properties:

document.cookie = newCookie
var x = document.cookie;
Test and see‹/›

You can find more information in ourJavaScript Cookies TutorialLearn more about cookies.

Browser compatibility

All browsers fully support the following cookie attributes:

Attribute
cookieisisisisis

Attribute value

ValueDescription
newCookie
newCookieis a string in the form ofkey=value. Please note that you can only set/Update a cookie.
Any cookie attribute value can be selected to update a cookie.key-valueAfter/Update the cookie and add a semicolon separator in front of it:
  • expires = date-(Optional) Specify the date in GMT format. If not specified, the cookie will be deleted when the browser is closed. (SeeDate.toUTCString()(Method)

  • path = path-(Optional) Instruct the browser of the path to the directory where the cookie belongs. The path must be absolute. If not specified, it defaults to the current path of the current document location.

  • domain = domain-(Optional) Specify the domain of the site (e.g., "example.com"). If not specified, it defaults to the host part of the current document location.

  • Secure -(Optional) Instruct the browser to send cookies to the server using a secure protocol (https).

Example of creating a cookie:

document.cookie="username=Seagull; expires=Thu, 27 Dec 2018 12:00:00 UTC; path=/";

Note: The cookie value string can be usedencodeURIComponent()to ensure that the string does not contain any commas, semicolons, or spaces (commas, semicolons, or spaces are not allowed in cookie values).

Technical Details

Return value:a string containing the “ key=value ” cookie for
DOM Version:DOM 2Level

More Examples

Set Cookie:

document.cookie = "name=Seagull";
document.cookie = "favorite_food=Polluted Air";
Test and see‹/›

HTML DOM Document Object