Simple jQuery script for EU cookie law using jQuery and Bootstrap

John Avis by | August 11, 2015 | Jquery/Javascript Bootstrap

I recently needed to display an implied consent EU cookie law message on a website which uses Bootstrap 3.x and jQuery.
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

Jquery/Javascript Responsive Web Design

Yet another enhancement of the simple Pinterest Style Grid Layout jQuery Plugin

by John Avis | November 16, 2017

I have made another change to the modified version of Mediademons Simple jQuery Plugin To Create Pinterest Style Grid Layout - Pinterest Grid.


Bootstrap Cool Websites

Bootstrap 4 beta Customiser

by John Avis | October 31, 2017

I love Bootstrap v4 and now that it is finally in Beta can start to use it in new projects. But I miss the online customizer that was available for Bootstrap v3.


Jquery/Javascript

Back to top button like Facebook app

by John Avis | September 15, 2017

The Facebook app on Windows phone has a nice and unobtrusive back to top button that only appears once you scroll past a certain point, and only when you start scrolling up the page. This code offers similar functionality using jQuery.

Comments

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

Leave a Comment
Tags
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP

About me

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

Subscribe

Get the latest posts delivered to your inbox.