Instantly share code, notes, and snippets.
Last active
February 27, 2017 08:29
-
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 gpbeer/6a0a9967074a8ef63b0243def89e3d21 to your computer and use it in GitHub Desktop.
Cookie notification with general settings
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
/************************************************** | |
* Start - Cookie Message | |
***************************************************/ | |
// Notification options | |
var option = { | |
"background": "#808080", // Hex | |
"text_color": "#ffffff", // Hex | |
"button_text": "ok", | |
"position": "bottom",// Default position is bottom | |
"message": "Les cookies assurent le bon fonctionnement de nos services. En utilisant ces derniers, vous acceptez l'utilisation des cookies.", | |
"font_size": "11", //px | |
"opacity": "80", // 1-100 | |
"link_text": "en savoir plus", | |
"link_url": "http://www.google.com", // If empty url option, the link will not be displayed | |
"link_target_blank": true // Open in new window - true or false | |
}; | |
// We execute and pass the banner options | |
if (window.attachEvent) | |
window.attachEvent('onload', createCookieBanner( option)); | |
else | |
if (window.addEventListener) | |
window.addEventListener('load', createCookieBanner( option),false); | |
function isIPaddress(ip){ | |
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip)) return true; | |
return false; | |
} | |
function getDomain(){ | |
var domain; | |
if(isIPaddress(window.location.hostname)) | |
domain = ""; | |
else{ | |
domain = window.location.hostname.split("."); | |
domain = "." + domain[domain.length-2] + "." + domain[domain.length-1]; | |
} | |
return domain; | |
} | |
function writeCookie(key, value, domain, path){ | |
var dateExpire = new Date(); | |
dateExpire.setMonth(dateExpire.getMonth() + 13); | |
document.cookie= key + "=" + value + "; expires=" + dateExpire.toUTCString() + "; domain=" + domain + ";" + "path=" + path + ";"; | |
} | |
function readCookie(key){ | |
var value = new Array(); | |
var allcookies = document.cookie; | |
// Get all the cookies pairs in an array | |
cookiearray = allcookies.split(';'); | |
// Now take key value pair out of this array | |
for(var i=0; i<cookiearray.length; i++){ | |
if (i > 0) | |
cookiearray[i] = cookiearray[i].substring(1); | |
value[cookiearray[i].split('=')[0]] = cookiearray[i].split('=')[1]; | |
} | |
return value[key]; | |
} | |
function createDom(htmlStr) { | |
var fragment = document.createDocumentFragment(), | |
temp = document.createElement('div'); | |
temp.innerHTML = htmlStr; | |
while (temp.firstChild) { | |
fragment.appendChild(temp.firstChild); | |
} | |
return fragment; | |
} | |
// Convert Hex to rgba with opacity capability | |
function convertHex(hex,opacity){ | |
hex = hex.replace('#',''); | |
r = parseInt(hex.substring(0,2), 16); | |
g = parseInt(hex.substring(2,4), 16); | |
b = parseInt(hex.substring(4,6), 16); | |
resultRgba = 'rgba('+r+','+g+','+b+','+opacity/100+')'; | |
return resultRgba; | |
} | |
function createCookieBanner(option) { | |
if(!readCookie('cookie-enabled')){ | |
// Banner string options : background, text_color, button_text, message, font_size, opacity | |
console.log(JSON.stringify(option)); | |
// If link_url exists we build the button | |
var link_button = ""; // Link empty | |
var link_target_blank = ""; // Link target empty | |
if(option.link_url) { | |
// Open in new window attribute | |
console.log(option.link_target_blank); | |
if(option.link_target_blank !== false) { | |
link_target_blank = " target='_blank' "; | |
} | |
// Build the href | |
link_button = "<a "+link_target_blank+" href='" + option.link_url + "' style='text-decoration: underline;' title='" + option.link_text + "' >" + option.link_text + "</a> "; | |
} | |
// Position conditional style | |
var banner_position = "top:auto;bottom:0;"; // Default position | |
if(option.position === 'top') { | |
banner_position = "bottom:auto;top:0;"; | |
} | |
var bannerWrapper = createDom("<div id='cookie-notification-wrapper' style='background: "+option.background+"; background-color: "+convertHex(option.background,option.opacity)+"; color: "+option.text_color+"; "+banner_position+" font-size: "+option.font_size+"px;width:100%; min-height:36px; font-family:Arial,Helvetica,sans serif; text-align: center; padding: 5px; z-index: 100000; position:fixed; left: 0;'><div class='cookie-notification-inner container' style='display:inline-block;'>"+option.message+" "+link_button+" <button type='button' style='color:"+option.text_color+";border:1px solid "+option.text_color+";font-size: "+option.font_size+"px;background:none; height: 26px; line-height:25px; outline: 0; -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; padding: 0 8px; cursor: pointer; position: relative; vertical-align: middle; display: inline-block;' id='cookie-notification-button' title='Fermer'>"+option.button_text+"</button></div></div>"); | |
body=document.body; | |
body.insertBefore(bannerWrapper,body.childNodes[0]); | |
setTimeout(function () {document.getElementById("cookie-notification-wrapper");}, 300); | |
document.getElementById('cookie-notification-button').onclick = function(){ | |
var p = document.getElementById("cookie-notification-wrapper"); | |
body.removeChild(p); | |
writeCookie("cookie-enabled", "1", getDomain(), "/"); | |
} | |
} | |
} | |
/************************************************** | |
* End - Cookie Message | |
***************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment