This is a small jQuery plugin for handling cookies. I wrote this because all other cookie plugins I could find where either dead links or to limited for my taste.
// get cookie
var name = $.cookie("name");
if (name === undefined) {
console.log("cookie not set");
}
// set cookie
$.cookie("name","John Smith");
// get all cookies
var all_cookies = $.cookie();
if ("name" in all_cookies) {
console.log(all_cookies.name);
}
// delete cookie
$.cookie("name",null);
// keep cookie for 2 days
$.cookie("name","John Smith",2);
// keep cookie until a certain date and restrict it to a certain path and domain
$.cookie("world","exists",new Date(2012,12-1,21),"/path","example.com");
// set/delete multiple cookies at once
$.cookie({
name: "John Smith",
occupation: null,
age: {
value: "30",
expires: 365,
secure: true
}
});
// remove cookie and test wheter it was set:
if ($.removeCookie("name")) {
console.log("name was set");
}
Get all cookies as a map.
NOTE: Cookie values that wheren't encoded using encodeURIComponent
might
get corrupted, because any read cookie is passed through decodeURIComponent
.
If this fails though it then will be tried to decoded it using unescape
and
if that also fails the raw cookie value will be returned.
Cookies set/read through this API will always be properly encoded/decoded.
Get value of a cookie. If the cookie is not set undefined
will be returned.
name
A string denoting the cookie to get.
Set or remove a cookie.
name
A string denoting the cookie to set.
value
The value to which the cookie shall be set as a string. You can pass
null
if you want to delete the cookie.
expires
The expiration date of the cookie as string or Date
object or
the number of days in which the cookie will expire (can be negative). If
false
is passed an expiration date in the very distant future (2000 years)
is used. If not given or true
is passed the cookie will expire at the end
of the browser session.
path
A string containing the path of the serve it should be sent to. If
true
is passed the current path name is used.
domain
A string containing the domain of the serve it should be sent to.
If true
is passed the current host is used.
secure
If true
the cookie is limited to encrypted transmissions.
Returns $
.
Same as above but pass all arguments but the name and value in a map.
Returns $
.
Same as above but pass all arguments but the name in a map.
Returns $
.
Same as above but set or remove multiple cookies at once. The keys of map the
are the names of the cookies and the values are strings, null
or settings
like above.
Returns $
.
Remove a cookie.
Returns true
if the cookie was set before, false
otherwise.
Remove a cookie with the given settings.
Returns true
if the cookie was set before, false
otherwise.
Remove a cookie with the given settings.
Returns true
if the cookie was set before, false
otherwise.
Public Domain. But it would be nice if you credit this github repository as the source somewhere.