Cookie Law Script

The script is extremely customisable by passing an inline object as a literal notation.  Implementation couldn’t be simpler, just upload the script and associated stylesheet to your server and link to them in the <head> tag of each page.  Download links can be found at the bottom of this page.

Cookies are broken up into three categories, allowing users to select multiple levels of cookie allowance.  For example, you may have basket cookies as required, login cookies as functional, and analytics cookies as targeted:

There are a number of properties, all of which are optional, which allow you to fully customise the prompt. The only properties that need to be filled in are requiredCookies, functionalCookies and targetedCookies.  Without these being set, there isn’t much use in having the script installed as it will simple be deleting all cookies required (apart from the cookie set by the script itself – don’t worry, this falls under the “required” category!)

If one or more categories have no values in them, the options will not appear in the settings.  No need to give your user settings that do nothing!

List of CookieLegislation Properties

  • header
    Sets the title text of the window.
    Default value:
  • text
    The message displayed in the window explaining what cookies are.
    Default value:
  • changeButtonText
    The text displaying on the “Cookie Settings” button.
    Default Value:
  • allowButtonText
    The text displaying on the “Allow Cookies” button.
    Default Value:
  • hideButtonText
    The text displaying on the “Hide Settings” button.
    Default Value:
  • infoLink
    The URL of the “information” link.
    Default Value:
  • infoText
    The anchor text for the link defined in infoLink.
    Default Value:
  • requiredCookies
    The array containing the names of required cookies.  By default, contains the name of the cookie used by cookie-law.js
    Default Value:
  • requiredTitle
    The text on the radio button to allow only required cookies.
    Default Value:
  • requiredDescription
    The description of required cookies.
    Default Value:
  • functionalCookies
    The array containing the names of functional cookies.
    Default Value:
  • functionalTitle
    The text on the radio button to allow both required and functional cookies.
    Default Value:
  • functionalDescription
    The description of functional cookies.
    Default Value:
  • targetedCookies
    The array containing the names of targeted cookies.
    Default Value:
  • targetedTitle
    The text on the radio button to allow all cookies.
    Default Value:
  • targetedDescription
    The description of targeted cookies.
    Default Value:

How does it work?

Technically, a simple piece of Javascript can’t prevent cookies from being placed.

This script creates a cookie called cookie_legislation_level. The cookie will hold a number between 1 and 3 – the level of cookies allowed by the user. So, 1=required, 2=functional and 3=targeted.

However, this script does the next best thing – it causes any cookies which haven’t been approved by the user to expire each time a new page is loaded. In order to prevent the cookies from being placed on the user’s PC, you will need to amend the code behind your site.

This part is slightly trickier – anywhere that a cookie is set, you can place a simple conditional statement around it, as per the following examples. Login cookies fall under the “required” type, so we would check if the user has allowed level 2 or higher cookies:

PHP

ASP

Downloads

Changelog

Cookie Law v1.1
** Important **
The code to enable this script has changed. Please see the example at the top of this page.

  • Improved user friendliness by removing disable button and adding a new “allow” button.
  • Backwards compatibility for IE7 and IE8.

Cookie Law v1.0

  • Initial Release

Have a suggestion or feature request? Leave a comment or contact me, and I’ll consider adding it to the script!

Want to be notified of updates? Subscribe to this post!

Found this script helpful? Please consider donating, or leave a link to your site in the comments – It’s always nice to see people using my code!

[paypal-donation]