What is the shortest, accurate, and cross-browser compatible method for reading a cookie in JavaScript?
Very often, while building stand-alone scripts (where I can't have any outside dependencies), I find myself adding a function for reading cookies, and usually fall-back on the QuirksMode.org readCookie()
method (280 bytes, 216 minified.)
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
It does the job, but its ugly, and adds quite a bit of bloat each time.
The method that jQuery.cookie uses something like this (modified, 165 bytes, 125 minified):
function read_cookie(key)
{
var result;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
}
Note this is not a 'Code Golf' competition: I'm legitimately interested in reducing the size of my readCookie function, and in ensuring the solution I have is valid.
Both of these functions look equally valid in terms of reading cookie. You can shave a few bytes off though (and it really is getting into Code Golf territory here):
All I did with this is collapse all the variable declarations into one var statement, removed the unnecessary second arguments in calls to substring, and replace the one charAt call into an array dereference.
This still isn't as short as the second function you provided, but even that can have a few bytes taken off:
I changed the first sub-expression in the regular expression to be a capturing sub-expression, and changed the result[1] part to result[2] to coincide with this change; also removed the unnecessary parens around result[2].
Assumptions
Based on the question, I believe some assumptions / requirements for this function include:
"foo:bar[0]"
should return a cookie (literally) named "foo:bar[0]";Under these assumptions, it's clear that
encodeURIComponent
/decodeURIComponent
should not be used; doing so assumes that the code that set the cookie also encoded it using these functions.The regular expression approach gets problematic if the cookie name can contain special characters. jQuery.cookie works around this issue by encoding the cookie name (actually both name and value) when storing a cookie, and decoding the name when retrieving a cookie.A regular expression solution is below.Unless you're only reading cookies you control completely, it would also be advisable to read cookies from
document.cookie
directly and not cache the results, since there is no way to know if the cache is invalid without readingdocument.cookie
again.(While accessing and parsing
document.cookies
will be slightly slower than using a cache, it would not be as slow as reading other parts of the DOM, since cookies do not play a role in the DOM / render trees.)Loop-based function
Here goes the Code Golf answer, based on PPK's (loop-based) function:
which when minified, comes to 128 characters (not counting the function name):
Regular expression-based function
Update: If you really want a regular expression solution:
This escapes any special characters in the cookie name before constructing the RegExp object. Minified, this comes to 134 characters (not counting the function name):
As Rudu and cwolves have pointed out in the comments, the regular-expression-escaping regex can be shortened by a few characters. I think it would be good to keep the escaping regex consistent (you may be using it elsewhere), but their suggestions are worth considering.
Notes
Both of these functions won't handle
null
orundefined
, i.e. if there is a cookie named "null",readCookie(null)
will return its value. If you need to handle this case, adapt the code accordingly.Shorter, more reliable and more performant than the current best-voted answer:
A performance comparison of various approaches is shown here:
http://jsperf.com/get-cookie-value-regex-vs-array-functions
Some notes on approach:
The regex approach is not only the fastest in most browsers, it yields the shortest function as well. Additionally it should be pointed out that according to the official spec (RFC 2109), the space after the semicolon which separates cookies in the document.cookie is optional and an argument could be made that it should not be relied upon. Additionally, whitespace is allowed before and after the equals sign (=) and an argument could be made that this potential whitespace should be factored into any reliable document.cookie parser. The regex above accounts for both of the above whitespace conditions.
this in an object that you can read, write, overWrite and delete cookies.
This will only ever hit document.cookie ONE time. Every subsequent request will be instant.
I'm afraid there really isn't a faster way than this general logic unless you're free to use
.forEach
which is browser dependent (even then you're not saving that much)Your own example slightly compressed to
120 bytes
:You can get it to
110 bytes
if you make it a 1-letter function name,90 bytes
if you drop theencodeURIComponent
.I've gotten it down to
73 bytes
, but to be fair it's82 bytes
when namedreadCookie
and102 bytes
when then addingencodeURIComponent
:How about this one?
Counted 89 bytes without the function name.