Skip to content

Instantly share code, notes, and snippets.

@aseredenko
Created August 3, 2016 09:49
Show Gist options
  • Save aseredenko/30eca6babda7e03aeb625f8732e64d90 to your computer and use it in GitHub Desktop.
Save aseredenko/30eca6babda7e03aeb625f8732e64d90 to your computer and use it in GitHub Desktop.
Function to wasy work with cookies (source: https://www.sitepoint.com/how-to-deal-with-cookies-in-javascript/)
function createCookie(name, value, expires, path, domain) {
var cookie = name + "=" + escape(value) + ";";
if (expires) {
// If it's a date
if(expires instanceof Date) {
// If it isn't a valid date
if (isNaN(expires.getTime()))
expires = new Date();
}
else
expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
cookie += "expires=" + expires.toGMTString() + ";";
}
if (path)
cookie += "path=" + path + ";";
if (domain)
cookie += "domain=" + domain + ";";
document.cookie = cookie;
}
function getCookie(name) {
var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
var result = regexp.exec(document.cookie);
return (result === null) ? null : result[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment