mikejmoran of Speak Creative
3/19/2019 - 7:45 PM

Hide Element for 24 hours with a Cookie

/*
You can see an example of this in action at the following URL: http://www.tmumc.org (active at the time of writing 05/10/17).
When you first go to the site, you will see an overlay that shows up, pointing them towards the App store app.
If you close this button, it sets a cookie on the users machine.
So then, if you refresh the page, the overlay will no longer show up for 24 hours.
*/



/* This piece checks to see if a cookie has been set. If it has not been set, then it adds the "active" class to the element, so that the element shows up. */

if (!readCookie('hide')) {
	$("#element-to-show").addClass("active");
}





/* This piece sets the cookie to hide the element, once they click on the close button */
$(".close-button-selector").click(function(){
	$("#element-to-show").removeClass("active");
	createCookie('hide', true, 1)
  return false;
});
	





/* The following are standard functions that need to be added into your JS file to read/write/delete cookies */

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}