jQuery Cookie : Add, Remove and Get Example

In this post I would like to share jQuery Cookie Plugin that will help you easily add/create/set, remove/erase/delete and get and basically manage your cookies. jquery.cookie is a simple, lightweight jQuery plugin for reading, writing and deleting cookies. Also read "How to create JSON cookies using jQuery"

How to use it?

//To set a cookie
$.cookie('the_cookie', 'the_value');

//Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });

//Create expiring cookie, valid across entire page:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

//Read cookie
$.cookie('the_cookie'); // => 'the_value'
$.cookie('not_existing'); // => null

//Delete cookie by passing null as value:
$.cookie('the_cookie', null);

// Creating cookie with all availabl options
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com',      secure: true, raw: true });

Available Options

  • expires: Define lifetime of the cookie. Value can be a Number (which will be interpreted as days from time of creation) or a Date object. If omitted, the cookie is a session cookie.
  • path: Define the path where cookie is valid. By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire page use path: '/'.
  • domain: Domain of page where the cookie was created.
  • secure: Default: false. If true, the cookie transmission requires a secure protocol (https).
  • raw: By default the cookie is encoded/decoded when creating/reading, using encodeURIComponent/ decodeURIComponent. Turn off by setting raw: true.

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...