Simple jQuery script for EU cookie law using jQuery and Bootstrap

johna by | August 11, 2015 | Jquery/Javascript Bootstrap Web Development

Simple jQuery script for EU cookie law using jQuery and Bootstrap

I recently needed to display an implied consent EU cookie law message on a website which uses Bootstrap 3.x and jQuery.

With a little inspiration from another post, Create a simple 'Implied Consent' EU Cookie Law Banner with JavaScript, I modified this to simplify it and adapt it for Bootstrap and jQuery.

var cookiePromptTest = false; //change this to true to test the message

$(function () {
if (cookiePromptTest || checkCookie("cookiePrompt") != "on") {
//header is the id of the element the message will appear before
$("#header").before('<div id="cookie-prompt" class="alert alert-danger"><button type="button" class="close" aria-label="Close" onclick="closeCookiePrompt()"><span aria-hidden="true">×</span></button>This website uses cookies. By continuing we assume your permission to deploy cookies, as detailed in our <a href="/cookies-policy/" class="alert-link" rel="nofollow" title="Cookies Policy">cookies policy</a>.</div>');
}
});

function closeCookiePrompt() {
if (!cookiePromptTest) {
createCookie("cookiePrompt", "on", 30); //don't show message for 30 days once closed (change if required)
}
$("#cookie-prompt").remove();
}

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 = "";
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);
}

See this script in action on this website.

Related Posts

Web Development Retro Computing

Converting dBase IV programs to run in the browser

by johna | September 13, 2024
Some pointless entertainment trying to get some old dBase programs running in the browser.

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment

About

...random postings about web development and programming, Internet, computers and electronics topics.

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.