Last active
February 15, 2024 19:20
-
-
Save miwebguy/cd58a484f362eda3df099b1bdee64bbe to your computer and use it in GitHub Desktop.
ConsentX Cookie Consent Banner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConsentX { | |
// based on Creare's 'Implied Consent' EU Cookie Law Banner v:2.4 | |
// Conceived by Robert Kent, James Bavington & Tom Foyster | |
constructor () { | |
this.dropCookie = true; // false disables the cookie, allowing you to style the banner | |
this.cookieDuration = 14; // days before the cookie expires, and the banner reappears | |
this.cookieName = 'complianceCookie'; // name of our cookie | |
this.cookieValue = 'on'; // initial value of cookie | |
this.cookieBannerText = ` | |
<button class="close-cookie-x" onclick="CX.removeMe();">X</button> | |
<p>We use cookies to ensure that we give you the best experience on our website. | |
Continued use of this site means you accept our | |
<a target='_blank' href="https://www.morrison.industries/privacy.html" rel="nofollow">Terms and Conditions</a>.</p> | |
<button class="close-cookies-btn" onclick="CX.removeMe();">Accept</button> | |
`; | |
this.cstyle = ` | |
#cookie-law{position:fixed;z-index:100;max-width:100vw;left:0;right:0;bottom:0;padding:1em; | |
background:rgba(0,0,0,0.95);text-align:center;color:#fff} | |
#cookie-law a {color:#fff} | |
.close-cookie-x {color:#fff;background:transparent;float:right} | |
.close-cookie-btn {} | |
`; | |
// load banner if cookie value not set | |
if(this.checkCookie(this.cookieName) != 'off'){ | |
this.createDiv(); | |
} | |
} | |
// create the banner | |
createDiv(){ | |
var bodytag = document.getElementsByTagName('body')[0]; | |
var cookieBannerStyle = document.createElement('style') | |
cookieBannerStyle.innerHTML = this.cstyle; | |
bodytag.insertBefore(cookieBannerStyle,bodytag.firstChild); | |
var div = document.createElement('div'); | |
div.setAttribute('id','cookie-law'); | |
div.setAttribute('role','dialog'); | |
div.innerHTML = this.cookieBannerText; | |
bodytag.insertBefore(div,bodytag.firstChild); | |
this.createCookie('on', window.cookieDuration); // Create the cookie | |
} | |
// create the cookie for accept | |
createCookie(value,days) { | |
console.log('cookie setting'); | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime()+(days*24*60*60*1000)); | |
var expires = "; expires="+date.toGMTString(); | |
} else var expires = ""; | |
if(this.dropCookie) { | |
document.cookie = this.cookieName+"="+value+expires+"; path=/"; | |
console.log('attempted cookie setting'); | |
} | |
} | |
// check the cooke value has been clicked | |
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; | |
} | |
//for testing reset cookie and reload banner | |
eraseCookie(name) { | |
this.createCookie("on",this.cookieDuration); | |
this.createDiv(); | |
} | |
// remove off the banner | |
removeMe(){ | |
this.createCookie("off",this.cookieDuration); | |
var element = document.getElementById('cookie-law'); | |
element.parentNode.removeChild(element); | |
} | |
}; | |
// load the banner if no cookie set | |
window.onload = function(){ | |
CX = new ConsentX() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment