Skip to content

Instantly share code, notes, and snippets.

@FredNandrin
Created March 27, 2020 09:35
Show Gist options
  • Save FredNandrin/155041f2d7e090895c46403e902e7ae8 to your computer and use it in GitHub Desktop.
Save FredNandrin/155041f2d7e090895c46403e902e7ae8 to your computer and use it in GitHub Desktop.
cookiebanner.js
// Based on code by Robert Kent, James Bavington & Tom Foyster
var dropCookie = true; // false disables the Cookie, allowing you to style the banner
var cookieDuration = 1; // Number of days before the cookie expires, and the banner reappears
var cookieName = 'Covid19'; // Name of our cookie
var cookieValue = 'on'; // Value of cookie
var cookieDivId = 'xxxx';
var cookieCloseButtonClass = 'yyyy';
function displayCovid19Banner() {
// TODO insert code to display the banner
$('#'+window.cookieDivId+' .'+window.cookieCloseButtonClass).click(function() {
createCookie(window.cookieName,window.cookieValue, window.cookieDuration); // Create the cookie
// TODO insert to hide the banner
});
}
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 = "";
if(window.dropCookie) {
document.cookie = name+"="+value+expires+"; path=/";
}
}
function checkCookie(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);
}
window.onload = function(){
if(checkCookie(window.cookieName) != window.cookieValue){
displayCovid19Banner();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment