Skip to content

Instantly share code, notes, and snippets.

@bigeasy
Created March 28, 2011 04:28
Show Gist options
  • Select an option

  • Save bigeasy/890004 to your computer and use it in GitHub Desktop.

Select an option

Save bigeasy/890004 to your computer and use it in GitHub Desktop.
Parse an HTTP Cookie
function setCookie(cookies, value) {
var values = value.split(/;\s+/);
var split = values.shift().split(/=/);
var key = split.shift();
cookies[key] = { value: split.join("=") };
values.forEach(function (n) {
var split = n.split(/=/);
var property = split.shift();
cookies[key][property] = split.length == 0 ? true : split.join("\n");
});
if (cookies[key].expires) {
cookies[key].expires = new Date(cookies[key].expires);
}
}
var cookies = {};
var value = "PREF=ID=816b0b009537df29:FF=0:TM=1301285972:LM=1301285972:S=TZE8UNJBWwNUTYUO; expires=Wed, 27-Mar-2013 04:19:32 GMT; path=/; domain=.google.com; secure"
setCookie(cookies, value);
console.log(cookies);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment