Skip to content

Instantly share code, notes, and snippets.

@DmitryDmitrienko
Created November 9, 2015 06:45
Show Gist options
  • Save DmitryDmitrienko/32bebb5df4147b76a969 to your computer and use it in GitHub Desktop.
Save DmitryDmitrienko/32bebb5df4147b76a969 to your computer and use it in GitHub Desktop.
function setCookie(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires == "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires * 1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for (var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment