If for whatever reason you can’t just use a nice jQuery plugin like js-cookie then the below Javascript will serve as a basic method of setting and getting cookies.

function setCookie(name, value, ms, path, domain) {
	if (!name || !value) {
	  return;
	}
	var d;
	var cpath = path ? '; path=' + path : '';
	var cdomain = domain ? '; domain=' + domain : '';
	var expires = '';
	if (ms) {
		d = new Date();
		d.setTime(d.getTime() + ms);
		expires = '; expires=' + d.toUTCString();
	}
	document.cookie = name + "=" + value + expires + cpath + cdomain;
}

setCookie('emailAddress', '[email protected]', 1800000, '/', undefined);
function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

var email = getCookie('emailAddress');

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *