Get list of all cookies using Javascript

Get list of all cookies using Javascript

Its very common that we all still use HTTP cookies for storing various data on visitor’s browser. But in HTML5, you can use local storage. I’m talking about the old school cookies here as we still have old browsers to take care of.

Following is the code snippet to log all cookies of the domain in the firebug console.

function get_cookies_array() {

    var cookies = { };

    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        }
    }

    return cookies;

}
var cookies = get_cookies_array();
for(var name in cookies) {
 console.log( name + " : " + cookies[name] + "<br />" );
}

You just need to copy the entire code and paste in the console and run it. Or else, you can also just copy paste and use your own way or alert on page load. Its just that simple! .

Print This Post    Email This Post
Posted on : 10th January 2012 by Castler

Leave a Reply

Comment moderation is enabled. Your comment may take some time to appear.