Cookie Permission Javascript for EU Cookie Law Compliance

EU Cookie Law

This code & article content is credited to Michael Wright

EU Cookie Law

You may of heard about the new EU Cookie law by now, as of May 26th 2012 all websites based in the UK (even if their site is hosted overseas) must seek consent to store cookies on a user’s computer, or device. Failure to comply could result in a fine up to £500,000 ($807,300).

Javascript Solution

Here is a Javascript solution that will add a small confirmation banner across the top of the page similar to the image above, requesting permission from the user to save and access Cookies from your website/domain. On future visits, you can simply check if they approved your cookies or not.

The idea is simple, a piece of JavaScript that can be thrown into any page, that can be used to automatically show the above banner to new visitors to check that you are allowed to store cookies. The ability is then in place so that you can simply check what stage of cookies have been accepted with the following line:


cPrompt.checkCookie()

4 possible returned values from cPrompt.checkCookie():

0 The user has actively opted out of all cookies on the site.
   Shows the red notification.
1 The user has seen a warning about cookies, but neither accepted nor declined, 
   this is classed as inferred  acception. Shows the blue notification.
2 The user has accepted all cookies to the site. Shows the green notification.
3 The user's first visit to the site, no cookies accepted or declined.
   Shows the yellow notifcation.

You should only store/use cookies if the above returns either 1 or 2. Also, the user is able to close the prompt that appears, and by them doing so it will show an icon in the bottom left hand corner of the page that they can click on for more information.

The Javascript file is available to download on Github

Using it around any block of code that stores cookies is as simple as doing this:


if(cPrompt.checkCookie() == 1 || cPrompt.checkCookie() == 2){
    /**
       Cookie Storing Code Here
    **/
}

There is an option to specify a URL to a cookie policy on your site, this can be done by using the following line of code:


cPrompt.cookieLink = 'http://mycookiepolicy.com/me.html';

June 30 2012

Written by

Web-Developer from Central Florida. This blog is my outlet to what I am into at the time. PHP, Javascript, MySQL, OO Practices, Performance, Cool Software and Services, etc... Please subscribe to the RSS feed

  • Johan Sundström

    That’s a silly API. If you insist on numbers instead of cPrompt.okay or so, turn 3 to -1, so the test becomes cPrompt.checkCookie() > 0.

    • http://www.tosbourn.com/ Toby Osbourn

      It is returning flags that happen to be integer values, it is short sighted to treat flags like numbers.

  • http://www.tosbourn.com/ Toby Osbourn

    Thanks for sharing this – that is one of the tidiest solutions I have seen. I really liked the fact that the images were stored inside the JavaScript, it is a fairly obvious thing to do but so many projects keep small images in a /imgs/ folder.

    • http://www.codedevelopr.com/ Jason Davis

      Credit is to Michael Wright for this project but I agree, I am all about cutting down on HTTP request when possible

  • http://www.facebook.com/jonny.rimes Jonny Rimes

    So what if javascript is disabled? You’re still not complying with the directive.

    • http://www.codedevelopr.com/ Jason Davis

      I’ts definitively not without flaws, this is really something the Browser vendors should and hopefully will handle eventually.  Using this it is assuming you are storing and working with the cookies with Javascript, so if used correctly and Javascript was disabled then it would not save or read the cookies.  Like I said this isn’t really ideal so hopefully the browser vendors will step up to the plate here.