Javascript – get all the cookies from the browser


I need to get all the cookies stored in my browser using JavaScript. How can it be done?

Best Solution

You can only access cookies for a specific site. Using document.cookie you will get a list of escaped key=value pairs seperated by a semicolon.


To simplify the access, you have to parse the string and unescape all entries:

var getCookies = function(){
  var pairs = document.cookie.split(";");
  var cookies = {};
  for (var i=0; i<pairs.length; i++){
    var pair = pairs[i].split("=");
    cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
  return cookies;

So you might later write:

var myCookies = getCookies();
alert(myCookies.secret); // "do not tell you"