[whatwg] HTML Cookie API

Garrett Smith dhtmlkitchen at gmail.com
Wed Feb 24 16:03:33 PST 2010


On Tue, Feb 23, 2010 at 8:56 PM, Adam Barth <w3c at adambarth.com> wrote:
> The document.cookie API is kind of terrible.  Web developers shouldn't
> have to parse a cookie-string or prepare a properly formated
> set-cookie-string.  Here's a proposal for an HTML cookie API that
> isn't as terrible:
>
> https://docs.google.com/Doc?docid=0AZpchfQ5mBrEZGQ0cDh3YzRfMTRmdHFma21kMg&hl=en
>
> I'd like to propose we include this API in a future version of HTML.
> As always, feedback welcome.
>
| Because HttpOnly cookies are inaccessible to script, the |httpOnly|
| attribute will usually be false.

Usually? When ever would HttpOnly be true?

Instead of "array like", I would like to rather have a NamedCookieMap,
which is sort of like a NamedNodeMap that the cookies can be retrieved
by an item or namedItem.

I would also rather like to see a `cookies` getter instead of
getCookies (just personally, style-wise I like that).

// Get a Cookie object or null.
var fooCook = document.cookies.namedItem("foo");

- or -
// Get a Cookie object or undefined.
var fooCook = document.cookies["foo"];

- or -

// Get a Cookie object or undefined.
var fooCookIndex = 2;
fooCook = document.cookies[fooCookIndex];

How about that?

A Cookes API makes working with cookies easier.

Working with cookies is important to support browsers that do not
implement new features such HTML 5 storage APIs.

For legacy browsers, a Cookie API can be easily emulated in script. A
common interface for new browsers and legacy browsers would have
consistent cross browser behavior and little disparity. The same
cannot be said for Storage APIs.

Where is the argument for making the API async?

Cookies have been synchronous for longer than I have been writing
javascript and to my knowledge, nobody has ever complained of that
being a problem. A common feature test is something like:

var isCookieSupported;
if(typeof document.cookie == "string") {
  document.cookie = "test=1";
  isCookieSupported = document.cookie.indexOf("test=1") !== -1;
}

That feature test relies on the fact that cookies are synchronous. If
the new proposed API is also synchronous, then the cookies feature
test will be possible as:-

var isCookieSupported;
if(typeof document.cookie == "string") {
  document.setCookie({name: "test", value : "1"});
  isCookieSupported = document.cookies.test === "1";
}

Asynchronous cookies that would mean that cookie setting tests would be harder.

Garrett



More information about the whatwg mailing list