Cookie Permission Javascript for EU Cookie Law Compliance

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 users 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:

4 possible returned values from cPrompt.checkCookie():

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:

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: