THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

HTML DOM cookie Property

Document Object Reference Document Object

Example

Get the cookies associated with the current document:

var x = document.cookie;
Try it Yourself »

Definition and Usage

The cookie property sets or returns all name/value pairs of cookies in the current document.

For more information about cookies, read our JavaScript Cookies Tutorial.


Browser Support

Property
cookie Yes Yes Yes Yes Yes

Syntax

Return the cookie property:

document.cookie

Set the cookie property:

document.cookie=newCookie

Property Values

Parameter Description
newCookie A String that specifies a semicolon-separated list of name=value pairs, or one name=value pair together with any of the following, optional, values:
  • expires=date - Optional. Specifies the date in GMT format (See the Date.toUTCString method). If not specified, the cookie is deleted when the browser is closed

  • path=path - Optional. Tells the browser what path to the directory the cookie belongs to, (e.g., '/', '/dir'). Note: The path must be absolute. If not specified, the cookie belongs to the current page

  • domain=domainname - Optional. Specifies the domain of your site (e.g., 'example.com', '.example.com' (includes all subdomains), 'subdomain.example.com'). If not specified, the domain of the current document will be used

  • secure - Optional. Tells the browser to use a secure protocol (https) for sending the cookie to the server

An example of creating a cookie:

document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

Note: The value of a cookie cannot contain commas, semicolons or whitespaces. However, you can use the encodeURIComponent() method to ensure that they don't

Technical Details

Return Value: A String, containing the name/value pairs of cookies in the document
DOM Version Core Level 2 Document Object

Related Pages

JavaScript Tutorial: JavaScript Cookies


Document Object Reference Document Object