Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Reading existing cookies

Cookies for a given host, represented as objects, can be enumerated as such:

let enum = Services.cookies.getCookiesFromHost("example.com");
while (enum.hasMoreElements()) {
  var cookie = enum.getNext().QueryInterface(Ci.nsICookie2); 
  dump(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n");
}

All cookies, regardless of host, can be enumerated using Services.cookies.enumerator rather than getCookiesFromHost().

The following code demonstrates how to set a cookie in Firefox.

Services.cookies.add(".host.example.com", "/cookie-path", "cookie_name", "cookie_value", is_secure, is_http_only, is_session, expiry_date);

See also

Document Tags and Contributors

 Last updated by: jhao,