jQuery makes cookies very straightforward and easy to use.
If you want to set a cookie, simply add this line of jQuery code:
$.cookie("example", "foo");
This cookie is set for the current path level and will be destroyed when the user closes their browser. If you want to make a cookie last longer, for example 10 days, do this:
$.cookie("example", "foo", { expires: 10 });
To make a cookie available for all paths on your domain, set the path to everything:
$.cookie("example", "foo", { path: '/' });
Or, if you want to isolate the cookie to a single path or directory, use this instead:
$.cookie("example", "foo", { path: '/admin' });
To get the value of the cookie, you could show it's value inside an alert box, like so:
alert( $.cookie("example") );
Or, put it inside a variable for later use:
var cookievalue = $.cookie('example');
Finally, to delete a cookie, simply set the value to null
. Note that simply setting an empty string will not remove the cookie, just clear its value.
$.cookie("example", null);