Instantly share code, notes, and snippets.
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save fastbook/4bcd0e1ea22635aa4794f87d5fb1b81d to your computer and use it in GitHub Desktop.
Simple EU Cookie Law Banner JavaScript
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
// Creare's 'Implied Consent' EU Cookie Law Banner v:2.4 | |
// Conceived by Robert Kent, James Bavington & Tom Foyster | |
// Put into a namespace and by Bjørn Rosell | |
// Also changed behaviour so you have to click accept to make the banner stay away. | |
// To make it behave like the original, set "createCookieWhenBannerIsShown" to true. | |
var CookieBanner = (function() { | |
return { | |
'createCookieWhenBannerIsShown': false, | |
'createCookieWhenAcceptIsClicked': true, | |
'cookieDuration': 14, // Number of days before the cookie expires, and the banner reappears | |
'cookieName': 'cookieConsent', // Name of our cookie | |
'cookieValue': 'accepted', // Value of cookie | |
'_createDiv': function(html) { | |
var bodytag = document.getElementsByTagName('body')[0]; | |
var div = document.createElement('div'); | |
div.setAttribute('id','cookie-law'); | |
div.innerHTML = html; | |
// bodytag.appendChild(div); // Adds the Cookie Law Banner just before the closing </body> tag | |
// or | |
bodytag.insertBefore(div,bodytag.firstChild); // Adds the Cookie Law Banner just after the opening <body> tag | |
document.getElementsByTagName('body')[0].className+=' cookiebanner'; //Adds a class tothe <body> tag when the banner is visible | |
if (CookieBanner.createCookieWhenBannerIsShown) { | |
CookieBanner.createAcceptCookie(); | |
} | |
}, | |
'_createCookie': function(name, value, days) { | |
var expires; | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime()+(days*24*60*60*1000)); | |
expires = "; expires="+date.toGMTString(); | |
} | |
else { | |
expires = ""; | |
} | |
document.cookie = name+"="+value+expires+"; path=/"; | |
}, | |
'_checkCookie': function(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; | |
}, | |
'_eraseCookie': function(name) { | |
CookieBanner._createCookie(name,"",-1); | |
}, | |
'createAcceptCookie': function() { | |
CookieBanner._createCookie(CookieBanner.cookieName, CookieBanner.cookieValue, CookieBanner.cookieDuration); // Create the cookie | |
}, | |
'closeBanner': function() { | |
var element = document.getElementById('cookie-law'); | |
element.parentNode.removeChild(element); | |
}, | |
'accept': function() { | |
CookieBanner.createAcceptCookie(); | |
CookieBanner.closeBanner(); | |
}, | |
'showUnlessAccepted': function(html) { | |
//alert(CookieBanner._checkCookie(CookieBanner.cookieName)); | |
//alert(document.cookie); | |
if(CookieBanner._checkCookie(CookieBanner.cookieName) != CookieBanner.cookieValue){ | |
CookieBanner._createDiv(html); | |
} | |
} | |
} | |
})(); | |
window.onload = function(){ | |
//var html = '<p>Our website uses cookies. By continuing we assume your permission to deploy cookies, as detailed in our <a href="/privacy-cookies-policy/" rel="nofollow" title="Privacy & Cookies Policy">privacy and cookies policy</a>. <a class="close-cookie-banner" href="javascript:void(0);" onclick="CookieBanner.accept();"><span>X</span></a></p>' | |
var html = '<div>' + | |
'This site uses cookies to enhance your experience, personalize content and advertisements, and ' + | |
'analyze traffic. We may share your information with our marketing and analytics partners. ' + | |
'For further information, please review our ' + | |
'<a href="">Privacy Policy</a>. ' + | |
'By clicking "Accept", you agree to our use of cookies and similar technologies.' + | |
'</div>' | |
// Add the accept button | |
html += '<div><a href="javascript:void(0);" onclick="CookieBanner.accept();"><span>Accept</span></a></div>'; | |
CookieBanner.showUnlessAccepted(html); | |
} | |
/* | |
CSS could be: | |
#cookie-law { | |
display: flex; | |
align-items: center; | |
position: fixed; | |
bottom: 0; | |
left: 0; | |
right: 0; | |
padding: 20px 5%; | |
background-color: #ccc; | |
z-index: 999; | |
} | |
#cookie-law > div { | |
} | |
#cookie-law > div:first-child { | |
padding-right: 40px; | |
} | |
#cookie-law > div:first-child > a { | |
color: inherit; | |
} | |
#cookie-law > div:last-child { | |
vertical-align: middle; | |
} | |
#cookie-law > div:last-child > a { | |
padding: 10px 20px; | |
background-color: rgb(226, 231, 144); | |
border-radius: 5px; | |
color: black; | |
text-decoration: none; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment