[whatwg] Suggestion: API for accessing cookies (document.cookies)

Ian Hickson ian at hixie.ch
Fri Feb 8 21:00:43 PST 2008

On Fri, 28 Dec 2007, Christian Schmidt wrote:
> In order to read, write and delete cookies from script most people use 
> their own utility functions like the ones shown at 
> http://www.w3schools.com/js/js_cookies.asp

Indeed. I think though that at this point this is well enough understood 
that it's not really worth changing the API -- people will have to support 
the old one for years anyway.

> It would be useful if cookies could be read, written and deleted through 
> a more abstract API, e.g. by an HTMLCollection-like interface stored in 
> document.cookies. This would allow something like this:
> if (document.cookies.myCookie) {
> 	alert(document.cookies.myCookie.value)
> 	document.cookies.remove('myCookie');
> 	// or alternatively
> 	document.cookies.myCookie.remove();
> 	document.cookies.add('myCookie2', 'myVal', '/', 'example.org');
> }
> In particular the remove() method would be useful, because today neither 
> the client-side nor the server-side can determine the path and domain 
> parameters necessary to delete a given cookie unless they know how it 
> was originally set.
> The API could give access to the values only or possibly also to the 
> other properties (expiry, path etc.) that are currenly not accessible 
> from neither client-side nor server-side.

Well, the DOM storage API and the SQL APIs provide a better API than 
cookies, but of course that data isn't sent over the network. A library 
could be made to interface them, though.

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list