Tuesday 12 June 2012

New unobtrusive EU Cookie Compliance Code

Some people have said that my JavaScript lightbox method for EU Cookie compliance is a bit off putting for users as it takes them away from the site if they disagree.

Also at the last minute the EU has allowed compliance with their new cookie privacy rules through a much easier method.

This allows sites to show the users that cookies are being used with a link to the sites cookie policy and a button to hide the message in future. If they continue to use the site they have basically agreed to the use of cookies.

You may have noticed on sites like the BBC or Channel 4 etc they will show a little piece of text at the top of the page that slides out and tells the user that cookies are being used with a link to their cookie policy. They also have a button to set a cookie so the message isn't shown again.

This method is a lot less intrusive and doesn't take the user away from the site as there is no "agree" or "disagree" button.

Therefore for those of you not wanting to make use of my EU Cookie compliance code I have created a newer version that follows this new format which can be seen here.

It is up to you to create a cookie policy document that details how cookies can be viewed and disabled through various browsers and toolbars etc. Plus the text and styling is up to you but the example html page shows this new unobtrusive method in action.

The code makes use of jQuery to handle the animations so load in the source code from your usual repository e.g Google or jQuery e.g: http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
The actual JavaScript for the html test page is below.

// run immediatley - place in footer

 EUCookie = {
  confirmed : false,

  Confirm : function(e){   
   var self = this;
   // create cookie   
   // slide back in the cookie bar
     height: 0
   }, 300, function(){
    $("#cookieWarning").css("display", "none");
   return false;

  CheckEUCookie : function(){

   var self = this,
    val = self.ReadCookie("EUCookie");
   // if our cookie has been set
   if(typeof(val)!=undefined && val==1){   
    self.confirmed = true;
   return self.confirmed;

  CreateCookie : function(name,value,days) {
   if (days){
    var date = new Date();
    var expires = "; expires="+date.toGMTString();
    var expires = "";
   document.cookie = name+"="+escape(value)+expires+"; path=/";

  ReadCookie : function(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){
     var r = unescape(c.substring(nameEQ.length,c.length));    
     return r;
   return null;

 // add click event;
 // if no cookie set show form
  // cookie already set so hide box
  $("#cookieWarning").css("display", "none");
  // Show the form - set to zilch then slide out to the appropriate height
  document.getElementById("cookieWarning").style.display = "none";
  document.getElementById("cookieWarning").style.height = 0;
     height: 28
   }, 500, function(){
  }).css("display", "block");    

To view the code in action you can visit the demo page and download the html page and tweak it to your hearts desire.

Let me know what you think.


Juergen, Germany said...

Hello Rob, I just found your EU cookie script and I am currently implementing it into our website, but to make it work with a more recent version of jquery I had to change some minor things:
I had to add this at the top of your Javascript:

var $j = jQuery.noConflict();

and then I changed every $ to $j
To exclude the script from XHTML 1.1 strict test, I had to include your script like this:

/* */

Now it works like charm with jQuery v1.10.2 and XHTML 1.1 strict validates properly.

Rob Reid said...


Cheer for the info.

I guess you are either loading in another framework like Prototype that uses the $ quick reference for jQuery otherwise you would not need that noConflict line. Another way would just to be to replace all $ with the word jQuery e.g jQuery("#cookieWarning").css("display", "none");

As for Strict XHTML reference I don't think people care that much about that now about it, just got to Google and run the HTML validator against it and see how many errors they have.

Before they moved to AJAX (type and search, which slows things down) they used to have compressed HTML with no quotes around attributes, no back slashes on self contained elements e.g <div id=main class=blah>. Also they still do just do things like <script>js=$</script> or <style>#div.me{color:red;}</style> so they care more about speed of loading and size than validation.

It's obviously up to you what you want to do but I'm just mentioning that even the biggest website in the world doesn't even care about validation that much.