Lightweight cookie-consent

Go to demo

There are a LOT of powerful cookie consent solutions out there, even free ones. I had tried a few of those free ones, but one way or another, they never fully met my own requirements. So I decided to "bake" one for myself.

My requirements

As per my needs, the solution had to be something relatively simple, highly optimized, no npm packages or other dependencies. A single .js plugin which must have had at least the following main features:

  • Easy management of scripts
  • Multilanguage support
  • GDPR compliant
  • Lightweight + vanilla JS
  • Not horrible looking

And ... so i did! A pure vanilla js solution which works in almost all browser - yea, IE too, although it's pretty much dead now.

Get started

The installation is pretty straightforward. Either download it or use it via cdn:

once you've added the script into your html, like so:

<script defer src="<path-to-js>/cookieconsent.js"></script>

the next step is need to initialize the plugin! You can choose to create another .js file which you will then add to the html page the same way like the script before, or choose to write it inline (to avoid that one additional http request):

Show code
<script defer src="<path-to-js>/cookieconsent.js"></script>

<!-- Inline script -->
    window.addEventListener('load', function(){
        const cc = initCookieConsent();

        // basic configuration{
            current_lang : 'en',
            autoclear_cookies: true,                   // default: false
            page_scripts: true,                        // default: false

            // mode: 'opt-in'                          // default: 'opt-in'; value: 'opt-in' or 'opt-out'
            // delay: 0,                               // default: 0
            // auto_language: null                     // default: null; could also be 'browser' or 'document'
            // autorun: true,                          // default: true
            // force_consent: false,                   // default: false
            // hide_from_bots: false,                  // default: false
            // remove_cookie_tables: false             // default: false
            // cookie_name: 'cc_cookie',               // default: 'cc_cookie'
            // cookie_expiration: 182,                 // default: 182 (days)
            // cookie_necessary_only_expiration: 182   // default: disabled
            // cookie_domain: location.hostname,       // default: current domain
            // cookie_path: '/',                       // default: root
            // cookie_same_site: 'Lax',                // default: 'Lax'
            // use_rfc_cookie: false,                  // default: false
            // revision: 0,                            // default: 0

            onFirstAction: function(user_preferences, cookie){
                // callback triggered only once

            onAccept: function (cookie) {
                // ...

            onChange: function (cookie, changed_preferences) {
                // ...

            languages : {
                en : {
                    consent_modal : {
                        title :  "I use cookies",
                        description :  'Your cookie consent message here',
                        primary_btn: {
                            text: 'Accept',
                            role: 'accept_all'  //'accept_selected' or 'accept_all'
                        secondary_btn: {
                            text : 'Settings',
                            role : 'settings'   //'settings' or 'accept_necessary'
                    settings_modal : {
                        title : 'Cookie settings',
                        save_settings_btn : "Save settings",
                        accept_all_btn : "Accept all",
                        reject_all_btn : "Reject all,
                        close_btn_label: "Close",
                        blocks : [
                                title : "Cookie usage",
                                description: 'Your cookie usage disclaimer'
                                title : "Strictly necessary cookies",
                                description: 'Category description ... ',
                                toggle : {
                                    value : 'necessary',
                                    enabled : true,
                                    readonly: true
                                title : "Analytics cookies",
                                description: 'Analytics description ... ',
                                toggle : {
                                    value : 'analytics',
                                    enabled : false,
                                    readonly: false

Note: This is a basic configuration, there are many other options you can configure!


With all of that done, you should see the cookieconsent pop into your screen!

Upcoming improvements

As of now the plugin is not very customizable (visually). You can change the color scheme easily thanks to css variables inside cookieconsent.css but that's pretty much it! (Unless you want to dive in yourself and inspect the style of the existing classes)

Update: added gui_options parameter for more layout options

Also, the plugin does not manage (yet!) iframes, so if you'd like to automatically block things like youtube embedded iframes, that's a no-go for now.

Update: created iframemanager plugin to easily manage iframes

Where to find it

If you want to give it a try, you can find the plugin on github . Contributions are very welcome!