document.cookie reads and writes cookies
The cookie property allows you to read, add to, and update the cookies associated with the document.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<script>
var cookieCount = 0;
//read Cookies
document.writeln(document.cookie);
//create Cookie
cookieCount++;
document.cookie = "Cookie_" + cookieCount + "=Value_" + cookieCount;
//read Cookies
document.writeln(document.cookie);
//update Cookie
document.cookie = "Cookie_" + cookieCount + "=Updated_" + cookieCount;
//read Cookies
document.writeln(document.cookie);
</script>
</body>
</html>
The additional fields that can be added to a cookie
Addition | Description |
---|---|
path=<path> | cookie path; default to the path of the current document if not specified. |
domain=<domain> | cookie domain; defaults to the domain of the current document if not specified. |
max-age=<seconds> | the life of the cookie in terms of the number of seconds. |
expires=<date> | Sets the life of the cookie using a GMT-format date. |
secure | The cookie will be sent only over a secure (HTTPS) connection. |
Each of these additional items is prepended to the name/value pair and separated with a semicolon:
document.cookie = "MyCookie=MyValue;max-age=10";
Home
JavaScript Book
DOM
JavaScript Book
DOM
Document:
- The Document Object
- document.body returns the body element as HTMLBodyElement
- document.characterSet returns document character set encoding
- document.charset gets or sets the document character set encoding
- document.compatMode
- document.cookie reads and writes cookies
- document.defaultCharset gets the default character encoding
- document.defaultView
- document.getElementsByTagName( tagName )
- document.getElementsByClassName ( className )
- document.getElementsByName ( nameOfNameAttribute )
- document.images gets all the img elements and returns HTMLCollection storing all images
- document.lastModified returns the last modified time of the document
- document.location returns the URL of the current document as Location class
- document.implementation property has information about the implementation of the DOM features
- document.querySelectorAll gets all of the elements that match the specified CSS selector
- document.readyState
- document.title returns the document title, changes the document title
- document.URL property returns the URL of the current document
- document.writeln() appends content to the end of the HTML document
- Using Properties to Obtain Element Objects