1. Introduction
This section is non-normative.Web Applications have conventionally had to poll document.cookie in order to know when a cookie has been modified or removed. In order to improve performance and reduce the use of unnecessary timers, this specification describes an API that allows for web developers to attach an event listener to `document.cookie`, and receive the relevant information about a cookie change via a callback. As a side effect of defining the data about the individual cookie that has changed, this document also creates an object based interface for getting and setting `document.cookie` values
2. Definitions
dictionary CookieInit
{
USVString name;
DOMString expires;
long long maxAge;
USVString domain;
USVString path;
boolean secure;
required USVString value;
};
A CookieInit
is a generic object that can be used to define a new cookie.
You can set the following fields:
name
is the key value in the key value pairing for cookies. Because of backwards compatibility, it is not required.expires
is an optional string value of a RFC 1123 Date, pursuant to RFC 6265§4.1.1maxAge
is an optional numeric value that, when specified, defines the number of seconds until the cookie expires, starting when the value is set, pursuant to RFC 6265§5.2.2domain
is an optional attribute that specifies what domain the cookie will be sent to, pursuant to RFC 6265§5.2.3.path
is an optional attribute that will only give access to the cookie if the requested resource matches the specified path, pursuant to RFC 6265§5.2.4.secure
is an optional boolean attribute that, when true, signals that the cookie can only be transmitted over "secure" protocols, as defined by the user agent. pursuant to RFC 6265§5.4, subsection 1value
is the only required attribute. It is a string, as defined as a cookie-value in RFC 6265
[Constructor
((CookieInit or USVString)cookie
), Exposed=Window] interfaceCookie
{ readonly attribute USVStringname
; attribute DOMStringexpires
; attribute USVStringvalue
; readonly attribute USVStringdomain
; readonly attribute USVStringpath
; readonly attribute booleansecure
; stringifier USVString (); };
For backwards compatibility, the stringifier for a Cookie
returns the serialized version of the object in a format
that is consistent with how a cookie is returned on the original document.cookie.
The Cookie can be set via a CookieInit
object, or via the same serialized string.
[Exposed=Window] interfaceCookieJar
: EventTarget { stringifier attribute USVString value; maplike<USVString, Cookie>; attribute EventHandler onchange; setter USVString ((Cookie or USVString)cookie
); };
CookieJar
represents all of the current cookies set on a domain, and is
available via `document.cookie`.
value
is a seralized version of the current valid cookies, in the same format as the historical value returns from `document.cookie`.onchange
is an event handler to listen for a `change` event (see: §3 the Change event)
partial interface Document {
[PutForwards=value] attribute CookieJar cookie
;
};
For backwards compatibility, a CookieJar
, when accessed as document.cookie,
returns the serialized string version of a Cookie
. However, a iterator
is also defined, which returns the object form of Cookie
on each iteration.
let cookie = document.cookie typeof cookie === 'string' // returns true for (let cookie of document.cookie) { if (cookie.name === 'cookieIWantToDelete') { doucment.cookie.delete(cookie) } }
3. the Change event
enum ChangeCause
{
"explicit",
"evicted",
"expired",
"expired-overwrite",
"overwrite",
"created"
};
A ChangeCause
is a string that represents the reason the cookie change
event has been fired.
explicit
is to be used when a delete() function has been called on a cookie called.evicted
is to be used when when a cookie has been automatically removed due to garbage collection.expired
is to be used when when a cookie’s max-age value expires, or when the expires value is in the past.expired-overwrite
is to be used when when a cookie’s max-age value is set to 0, or when the expires value is overwritten with a value in the pastoverwrite
is to be used when when a cookie’s value is overwritten.created
is to be used when when a cookie is created.
[Constructor
(DOMStringtype
, optional CookieChangeEventIniteventInitDict
), Exposed=(Window)] interfaceCookieChangeEventInit
: Event { attribute booleanremoved
; attribute ChangeCausecause
; attribute Cookiecookie
; };
interface CookieChangeEvent
: Event {
readonly attribute boolean removed;
readonly attribute ChangeCause cause;
readonly attribute Cookie cookie;
};
A CookieChangeEvent
is an event that is fired when a ChangeCause
has been triggered.
removed
is a boolean that represents whether or not a cookie has been removed from theCookieJar
cause
is a validChangeCause
, representing why a cookie the cookie in question has been modified.cookie
is a validCookie
, that represents the cookie that has been modified.