(function(){
anonymous (no name) function (the parentheses wrapping the function make it an expression rather than declaration) - defined and executed immediately (contained functions, variables, etc are removed from the current scope as soon as it finishes)
const cookieName = '_myCookieConsent_'
const path = '/'
const cookieValue = 'OK'
const expiryDays = 365
function setCookie(_n, _v, _d){
set cookie with _n name, _v value & expiry date _d days in the future
const _e = new Date(Date.now() + (_d * 24 * 60 * 60 * 1000)).toUTCString()
create _e expiry date, _d days from now (Date.now() returns number of milliseconds since 00:00 on 1 January 1970)
document.cookie = _n + '=' + _v + '; expires=' + _e + '; path=' + path
write the cookie (this would overwrite a cookie with the same name in the same path)
}
function dismissBanner(){
when clicking the 'OK' button
setCookie(cookieName, cookieValue, expiryDays)
set the '_myCookieConsent_' cookie
document.querySelector('.c-div').remove()
delete the div with class="c-div"(which removes the whole cookie banner overlay)
}
function getCookieValue(_n){
get the value of the cookie named _n
const nameEQ = _n + '='
const ca = document.cookie.split(';')
get the document.cookie and split it into an array of name=value elements
for (let i=0; i<ca.length; i++){
for each array element
let c = ca[i]
c = current array element
while (c.charAt(0) === ' ') c = c.substring(1, c.length)
while the 1st character of the string is a space, delete it
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length)
if the beginning of the string matches "_n=", return the cookie value
}
return null
if there’s no cookie or the _n cookie isn’t there, return null
}
if (getCookieValue(cookieName) !== cookieValue){
if our cookie “_myCookieConsent_” does not = “OK”, create the html for the cookie banner
const cDiv = document.createElement('div')
create a div element
cDiv.classList.add('c-div')
give it the class "c-div"
document.body.append(cDiv)
add it just before the page’s </body> tag
const cContent = document.createElement('div')
create a div element
cContent.classList.add('c-content')
give it the class "c-content"
cDiv.append(cContent)
add it to the end of the above div
cContent.append('Your message about cookies goes here.')
add in your cookie message text
const cButton = document.createElement('button')
create a button element
cButton.classList.add('c-OK')
give it the class "c-OK"
cContent.append(cButton)
add it to the end of the content div
cButton.textContent = 'OK'
text for the button
cButton.addEventListener('click', dismissBanner)
click the button to dismiss the banner (set the cookie and delete the overlay)
}
})()
this final pair of parentheses invokes the function (and would pass it arguments if there were any)
This script will create the html for a cookie banner, but only if the consent cookie is not set.
Again, this example has been kept relatively simple - you can keep adding elements for a more involved cookie banner