Skip to content

Instantly share code, notes, and snippets.

@tracker1
Created February 19, 2015 20:09
Show Gist options
  • Save tracker1/a1418769dc18feedef38 to your computer and use it in GitHub Desktop.
Save tracker1/a1418769dc18feedef38 to your computer and use it in GitHub Desktop.
getcookies.js
//requires ES5, or a shimmed browser for map/reduce
module.exports = function getCookies() {
if (!(this.document && this.document.cookie)) return {}; //no cookies for you
return this.document.cookie.split(/;\s+/g)
.map(function(x){
var kv=x.match(/([^\=]*)\=(.*)/);
return [kv[1]||'',kv[2]||''].map(decodeURIComponent);
})
.reduce(
function(v,kv){
v[kv[0]]=kv[1];
return v
}
,{}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment