jQuery Cookie - Cookies Management Plguin

jQuery Cookie - Cookies Management Plguin
File Size: 15.2 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

jQuery Cookie is a Cookies Management plugin that make it easy to read, write and delete cookies in the web app.

Note that this plugin is currently stopped updating. I highly recommend using js-cookie library instead.

You might also like:

How to use it:

1. Include the script after the latest jQuery library.

<script src="/path/to/cdn/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

2. Usages.

// Create session 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 site:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

// Read cookie:

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

// Delete cookie:
// Returns true when cookie was found, false when no cookie was found...
$.removeCookie('the_cookie');

// Same path as when the cookie was written...
$.removeCookie('the_cookie', { path: '/' });

Changelog:

v1.4.1 (2014-04-28)

v1.4.0 (2013-10-06)

  • Support for AMD.
  • Removed deprecated method $.cookie('name', null) for deleting a cookie, use $.removeCookie('name').
  • $.cookie('name') now returns undefined in case such cookie does not exist (was null). Because the return value is still falsy, testing for existence of a cookie like if ( $.cookie('foo') ) keeps working without change.
  • Renamed bower package definition (component.json -> bower.json) for usage with up-to-date bower.
  • Badly encoded cookies no longer throw exception upon reading but do return undefined (similar to how we handle JSON parse errors with json = true).
  • Added conversion function as optional last argument for reading, so that values can be changed to a different representation easily on the fly.

v1.3.1 (2013-03-24)

  • Fixed issue where it was no longer possible to check for an arbitrary cookie, while json is set to true, there was a SyntaxError thrown from JSON.parse.
  • Fixed issue where RFC 2068 decoded cookies were not properly read.

This awesome jQuery plugin is developed by carhartl. For more Advanced Usages, please check the demo page or visit the official website.