Top 5: Best JavaScript Libraries for Cookies Manipulation

Top 5: Best JavaScript Libraries for Cookies Manipulation

I am not the only one that says that manipulating cookies in JavaScript can be painful, or am I? According to the standart method for the creation, edition or to edit a cookie, everything is done through the cookie variable in the browser and his value is a plain string:

document.cookie = "username=Our Code World; expires=Thu, 19 Nov 2040 14:00:00 UTC; path=/";

Pretty weird isn't? Cookies are small "files" stored in the browser. They are designed to hold a modest amount of data (only a string). This allows the server to deliver a page tailored to a particular user, or the page itself can contain some script which is aware of the data in the cookie and so is able to carry information from one visit to the website (or related site) to the next. This become very useful when you want to share information between your server and the client's browser as you don't need to post such information using an HTTP format like GET, POST, PUT etc. In this article we'll share with you the most useful libraries to work with cookies easily in a readable and developer friendly way in your web project.

5. JeCookie

JeCookie is a simple script for easy cookie manipulation. It supports the following browsers:

  • IE 6+
  • FF 3+
  • Chrome 10+
  • Opera 10+

It's pretty easy to use and understand:

var cookie = new jecookie('cookie_name');

var cookie = new jecookie('cookie_name', {
     name : 'value'
}, {
    secure :true,
    domain : '.example.org'
});

4. Cookies.js

Cookies.js is a complete cookies reader/writer with full Unicode support. As cookies are just specially formatted strings it is sometimes difficult to manage them. This library aims to abstract the access to document.cookie by defining an object (docCookies) that is partially consistent with a Storage object. It also offers full Unicode support.

3. Cookie.js

Cookie.js is a tiny JavaScript library that simplifies cookies. It is capable of setting, getting and removing cookies, accepts a variety of parameters, and supports chaining. It doesn't have any dependencies and minified+gzipped it's only 900 Bytes small. Working with cookies in JavaScript sucks (as mentioned at the beginning). document.cookie is definitely one of the ugly parts of JavaScript. This library aims to provide an easy and nevertheless powerful way to use cookies. You can use the cookie.set method to set cookies. The value will automatically be escaped for you:

cookie.set('key', 'value');

2. Cookies

Cookies is a super simple cookie manipulation library written in JavaScript. Unlike other libraries where you define the key as a parameter of some function, with cookies you can work with cookies as if they were key inside an object, features that makes really easy to write and read cookies:

var userdata = { email: 'test@test.com', token: '42' };
cookies({ user: userdata, computer: "Desktop"});

You can also set complex data and it will be json-encoded saved and retrieved properly in a single cookie.

1. JsCookie (formerly jQuery Cookie)

JsCookie is a very simple, powerful yet lightweight JavaScript API for handling cookies:

If you know another awesome and easy to use JavaScript library to work with Cookies in the browser, please share it with the community in the comment box.

Happy coding !

This could interest you

Become a more social person