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:
    [html]This site uses cookies.[/html]
  • text
    The message displayed in the window explaining what cookies are.
    Default value:
    [html]<p>The cookie settings on this website are set to ‘allow all cookies’ to give you the very best experience.  If you continue without changing these settings, you consent to this – but if you want, you can change your settings at any time at the bottom of this page.</p>[/html]
  • changeButtonText
    The text displaying on the “Cookie Settings” button.
    Default Value:
    [html]Cookie Settings[/html]
  • allowButtonText
    The text displaying on the “Allow Cookies” button.
    Default Value:
    [html]Allow Cookies[/html]
  • hideButtonText
    The text displaying on the “Hide Settings” button.
    Default Value:
    [html]Hide Settings[/html]
  • infoLink
    The URL of the “information” link.
    Default Value:
  • infoText
    The anchor text for the link defined in infoLink.
    Default Value:
    [html]More information on cookies[/html]
  • requiredCookies
    The array containing the names of required cookies.  By default, contains the name of the cookie used by cookie-law.js
    Default Value:
    [html]new Array(“cookie_legislation_level”)[/html]
  • requiredTitle
    The text on the radio button to allow only required cookies.
    Default Value:
    [html]Allow required cookies[/html]
  • requiredDescription
    The description of required cookies.
    Default Value:
    [html]Only allow cookies which are necessary for the site to function.  Some features may not work.[/html]
  • functionalCookies
    The array containing the names of functional cookies.
    Default Value:
    [html]new Array()[/html]
  • functionalTitle
    The text on the radio button to allow both required and functional cookies.
    Default Value:
    [html]Allow functional cookies[/html]
  • functionalDescription
    The description of functional cookies.
    Default Value:
    [html]Also allow cookies which aid functionality of the site.  The site will be fully functional.[/html]
  • targetedCookies
    The array containing the names of targeted cookies.
    Default Value:
    [html]new Array()[/html]
  • targetedTitle
    The text on the radio button to allow all cookies.
    Default Value:
    [html]Allow targeted cookies[/html]
  • targetedDescription
    The description of targeted cookies.
    Default Value:
    [html]Also allow cookies to interact with other sites, for example social networks.[/html]

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 mark=”1,3″]if($_COOKIE[“cookie_legislation_level”] >= 2) {
setcookie(“login_session”, $userSession);

[vb mark=”1,3″]If Response.Cookie(“cookie_legislation_level”) >= 2
Response.Cookie(“login_session”) = userSession
End If[/vb]



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!