Writing and reading cookies

Note that as of 2019, Safari will prevent 1st party cookies from expiring later than 7 days. https://alightanalytics.com/alight-insights/safari-first-party-cookies/
JavaScript

Pass the cookie name to reading the value

function getCookie(c_name) {
	if (document.cookie.length>0){ 
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
};


Writing the cookie by passing the name, value and the number of days before it expires. Set exdays to zero for session cookies.

function setCookie(cname, cvalue, exdays) {
	if (exdays==0){
	    document.cookie = cname + "=" + cvalue + "; path=/";
	}else{
	    var d = new Date();
	    d.setTime(d.getTime() + (exdays*24*60*60*1000));
	    var expires = "expires="+ d.toUTCString();
	    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
	}
}

Posted by fbrefere001 on Tuesday August 27, 2002