Skip to content

Instantly share code, notes, and snippets.

@robisatthefunction
Last active January 25, 2022 17:56
Show Gist options
  • Save robisatthefunction/644f667814113f349101b921b88f0d15 to your computer and use it in GitHub Desktop.
Save robisatthefunction/644f667814113f349101b921b88f0d15 to your computer and use it in GitHub Desktop.
// setCookie('name of cookie', 'value', # days till expire, 'your site domain')
var setCookie = function(c_name,value,exdays,c_domain) {
c_domain = (typeof c_domain === "undefined") ? "" : "domain=" + c_domain + ";";
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value + ";" + c_domain + "path=/";
};
var getCookie = function(name) {
var match = document.cookie.match(name+'=([^;]*)');
if (!match) {
//configure to the value and expiration in days you would like and change to your domain
setCookie(name,'1',90,'.domain.com');
}
};
getCookie('nameOfCookie');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment