Simple jQuery script for EU cookie law using jQuery and Bootstrap

By · August 11, 2015 · 0 comments

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)

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.

Get the latest posts delivered to your inbox.


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

Leave a Comment

All comments are moderated and rel="nofollow" is in use. Avatars are sourced from – a globally recognised avatar.

Type the numbers from the picture above

About me
John Avis ...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

profile for John at Stack Overflow, Q&A for professional and enthusiast programmers

Get the latest posts delivered to your inbox. *