Skip to content

Instantly share code, notes, and snippets.

@Duobe
Forked from jameshartig/underscoreCookie.js
Last active December 29, 2018 10:35
Show Gist options
  • Save Duobe/ef470dbfe61a6d8c1877c1e967122364 to your computer and use it in GitHub Desktop.
Save Duobe/ef470dbfe61a6d8c1877c1e967122364 to your computer and use it in GitHub Desktop.
_.cookie
/**
* Gets or sets cookies
* @param name
* @param value (null to delete or undefined to get)
* @param options (domain, expire (in days))
* @return value or true
*/
_.cookie = function(name, value, options)
{
if (typeof value === "undefined") {
var n, v,
cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
n = $.trim(cookies[i].substr(0,cookies[i].indexOf("=")));
v = cookies[i].substr(cookies[i].indexOf("=")+1);
if (n === name){
return unescape(v);
}
}
} else {
options = options || {};
if (!value) {
value = "";
options.expires = -365;
} else {
value = escape(value);
}
if (options.expires) {
var d = new Date();
d.setDate(d.getDate() + options.expires);
value += "; expires=" + d.toUTCString();
}
if (options.domain) {
value += "; domain=" + options.domain;
}
if (options.path) {
value += "; path=" + options.path;
}
document.cookie = name + "=" + value;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment