Code Monkey home page Code Monkey logo

cookie-banner's Introduction

Cookie Banner Build Status

Cookie Banner is a super-easy way to ensure you're complying with the EU cookie law. Just reference the cookiebanner.min.js script from your page and you're done.

Cookie Banner script is very lightweight and depends on no JavaScript libraries, css files or images.

Demo

To see the script in action download the files and open/serve tests/demo.html and/or tests/tests.html.

Quickstart

Add a single line to your web page, just before the closing </body> tag:

<script type="text/javascript" id="cookiebanner"
  src="https://cdn.jsdelivr.net/gh/dobarkod/[email protected]/dist/cookiebanner.min.js"></script>

or via cdnjs:

<script type="text/javascript" id="cookiebanner"
  src="https://cdnjs.cloudflare.com/ajax/libs/cookie-banner/1.2.2/cookiebanner.min.js"></script>

This will display a black-and-white floating banner at the bottom of your web page, informing the user that the site is using cookies, and giving them the link to a page with more information.

When the user clicks the "close" button, the banner will set a cookie (oh, the irony!) remembering that the banner was acknowledged, so the same user will not be bothered again.

Options

If you don't like the defaults, you can modify the banner content and style. The following options are settable through a data- property on the script tag:

  • height - Banner/notice height (default: 32px)
  • fg - Banner/notice text color (default: #ddd)
  • bg - Banner/notice background color (default: #000)
  • link - Link text color (default: #aaa)
  • position - Banner/notice position, top or bottom (default: bottom)
  • padding - Set custom padding (default: 5px 16px)
  • message - Notice message text
  • linkmsg - Link text content (default: Learn more)
  • close-text - Text/symbol for the .cookiebanner-close element (default: &#10006;)
  • close-style - CSS style for .cookiebanner-close element (default: float:right;padding-left:5px;)
  • close-precedes - Controls whether .cookiebanner-close element precedes or follows the <span> containing the message (default: true)
  • font-size - Text/font size for the .cookiebanner (container) element (default: 14px)
  • font-family - Font family for the .cookiebanner (container) element (default: arial, sans-serif)
  • text-align - Text align/position for the .cookiebanner (container) element (default: center)
  • effect - Effect used when inserting the notice, currently only fade is supported (triggered when cookiebanner is displayed and when it is closed) (default: null)
  • fade-out-duration-ms - Duration in milliseconds of fade-out effect (only if fade effect is specified, default: 2000)
  • cookie - Name for the cookie that stores acceptance info (default: cookiebanner-accepted)
  • expires - Cookie expiry date/time (defaults to Infinity aka Fri, 31 Dec 9999 23:59:59 GMT). There's basic support for specifying a callback function (more flexibility, must return one of Number, String, or Date -- see Cookies.set()). You can also just specify a valid UTC string. When specified as a "numeric string", it is treated as number of seconds from now. So, expires: '90' or data-expires="90" would expire the cookie in 90 seconds.
  • cookie-path - Path to set for the cookie
  • cookie-domain - Set a custom cookie domain (default: null)
  • cookie-secure - Set (true/false) secure cookie for HTTPS (default: false)
  • moreinfo - Link where the visitor can read more about cookies (default: http://aboutcookies.org)
  • moreinfo-target - Target for moreinfo link (default: _blank)
  • moreinfo-decoration - Text decoration for moreinfo link (default: none)
  • moreinfo-font-weight - Font weight for moreinfo link (default: normal)
  • moreinfo-font-size - Font size (i.e 12px) for moreinfo link (default: null)
  • moreinfo-rel - Rel attribute value for moreinfo link (default: noopener noreferrer). Set to false or `` if you wish to disable it.
  • moreinfo-class - CSS class for moreinfo link (default: null). If present, it will be used to detect the moreinfo link (instead of getting the first a element within message). Use when your message has more than one link and you do not want the first one to have moreinfo link set.
  • mask - Controls whether a mask is created over the viewport (default: false). Clicking anywhere on the mask is considered as acceptance.
  • mask-opacity - Opacity used for the window mask (default: 0.5)
  • mask-background - CSS background style applied to the mask
    (default: #000)
  • zindex - Z-index set on the notice (default: 255). If mask is used, the notice
    's z-index is automatically incremented by 1 so it appears above the mask.
  • accept-on-scroll - When true, agrees and closes the notice when window is scrolled. (default: false)
  • accept-on-click - When true, agrees and closes the notice when clicking anywhere on the page. (default: false)
  • accept-on-first-visit - When true, agrees automatically (which stops showing the notice for subsequent requests), but the notice is not automatically closed. (default: false)
  • accept-on-timeout - Automatically agrees and closes the notice after specified number of milliseconds. (default: null)
  • on-inserted - A function which gets executed after the banner is inserted in the DOM (default: null)
  • on-closed - A function which gets executed after the banner is closed (default: null)
  • delay-before-close - Wait N milliseconds before closing the notice. (default: null)
  • debug - When true, closes the banner without setting the cookie (default: false)

Here's an example:

<script type="text/javascript" id="cookiebanner"
    src="https://cdn.jsdelivr.net/gh/dobarkod/[email protected]/dist/cookiebanner.min.js"
    data-height="20px" data-position="top"
    data-message="We use cookies to improve your browsing experience.">
</script>

Self-hosting

If you don't want to depend on cdnjs or jsdelivr, copy the cookiebanner.min.js file directly to your site and serve it yourself.

If you want to compile cookiebanner with the rest of your application's javascript, you can call it with options as follows:

<script type="text/javascript">
      var options = { message: "We use cookies to enhance your experience.", moreinfo: "/about/cookies" };
      var cb = new Cookiebanner(options); cb.run();
</script>

Internals and more options

If the banner needs to be shown, the script will create the following DOM subtree by default and add it just before the closing </body> tag:

<div class="cookiebanner">
    <div class="cookiebanner-close" style="float: right; padding-left:5px;">&#10006;</div>
    <span>Message. <a rel="noopener noreferrer" href=".." target="_blank">Learn more</a></span>
</div>

If the close-precedes option is sto to false (added in 1.2.1), the DOM looks slightly different:

<div class="cookiebanner">
    <span>Message. <a rel="noopener noreferrer" href=".." target="_blank">Learn more</a></span>
    <div class="cookiebanner-close" style="float: right; padding-left:5px;">&#10006;</div>
</div>

You can use CSS with div.cookiebanner > span and div.cookiebanner > a to further modify the banner appearance.

You can also try customizing the close button via the .cookiebanner-close CSS class. Keep in mind that you might have to override and/or reset certain properties by using !important CSS rules.

Event-Handlers (Callbacks)

There are two ways you can utilize event handlers like i.e. on-inserted.

Within JS (recommended):

<script type="text/javascript">
      var options = { onInserted: function (instance) { console.log('Hey, I got inserted!') } };
      var cb = new Cookiebanner(options); cb.run();
</script>

As a data-attribute:

<script type="text/javascript" id="cookiebanner"
    src="https://cdn.jsdelivr.net/gh/dobarkod/cookie-banner/dist/cookiebanner.min.js"
    data-on-inserted="function (instance) { console.log('Hey, I got inserted!'); }">
</script>

Hacking

Get the newest and the freshest from GitHub:

git clone https://github.com/dobarkod/cookie-banner.git

If you've modified the code, it's recommended you run it through linter to catch potential errors, and minifier to minimize its footprint.

We're old-school here so we just use Makefile for the tasks:

make lint  # run jshint to check the code
make test  # run the tests
make # minify it

You'll need jshint, eslint and uglifyjs (v2) tools installed for this, and also phantomjs if you want to run the automated tests.

Pull requests are welcome! In order to get your pull-request accepted, please follow these simple rules:

  • all code submissions must pass cleanly (no errors) with make test
  • there should be no external JavaScript, CSS, image files or any other dependencies
  • the code should work with no errors or warnings on recent Chrome, Firefox, Safari, Internet Explorer browsers and iOS and Android platforms
  • if you want to add significant features, file an issue about it first so we can discuss whether the addition makes sense for the project

cookie-banner's People

Contributors

adirael avatar akmur avatar aleksandrpanteleymonov avatar clarkewd avatar coil avatar coire1 avatar dependabot[bot] avatar janza avatar kinsi55 avatar ronan-mch avatar senko avatar stormleoxia avatar webaddicto avatar wottpal avatar zytzagoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cookie-banner's Issues

Android

doesn't work, no banner

Chrome for Android, OS 4.4.4 KitKat, Vodafone Smart Tab 4G

Support cookie expiration

For the moment set cookies are forever. We should expose the cookie end date/time as a setting for people which want to set cookies with a shorter lifespan.

The modified cookie banner doesn't work!

Hey hey, this code works:

<script type="text/javascript" id="cookiebanner" src="http://cookiebanner.eu/js/cookiebanner.min.js"></script>

But when I put the example in my html-file, it doesn't work! Also when I change the properties and/or the source (because they are different in the lines), it doesn't work either

What is wrong with that? Or what is wrong with me?
.

Thanks in advance and nice greetings from Germany,
Steffen =)

Styling option not working

The option to style the close icon has no effect.

Option name: close-style

<script type="text/javascript" id="cookiebanner" src="/scripts/cookiebanner.min.js" data-zindex="1000" data-moreinfo="/cookie-politik/" data-linkmsg="Få mere at vide" data-message="cookietext" data-close-style="padding-left:15px" data-close-text="X" ></script>

data-on-closed="" seems to loop?

I tried cookiebanner with this option:

data-on-closed="window.location.reload(false);"

So when the banner is closed it can reload the page.

But it seems it enters in a loop and always reload the page?

Empty linkmsg causes error

Hello,

I have noticed that an empty linkmsg value causes a javascript error.
I know how to get it fixed, do you want me to fix it or is this on purpose perhaps?

I was thinking that even better would be to add a "showLinkmsg" property so that if it is set to no, the link will simply not show.

Let me know,

Alex

data-text-align not work

Hi @dobarkod,
I'm using your script, powerful and easy. But I am having a problem, I am trying center the text with data-text-align, but it don't work, the text continue align on the left. Here is the code that I build:

<script type="text/javascript" id="cookiebanner"
    data-height="50px" data-bg="#22313F"
    data-message="Siento interrumpir tu navegación, pero me veo en la obligación de avisarte
    de que en este sitio web se usan cookies para mejorar tu experiencia (¡aunque muy pocas).
    Si continúas navegando estarás aceptando esto."
    data-text-align="right" data-linkmsg="¡Infórmate sobre las cookies!"
  src="http://cookiebanner.eu/js/cookiebanner.min.js"></script>

Everything works correct, except that attribute. Here you have a fiddle like it appears to me: on the left.

suggestion: change URL from http to https

hello!
After analysing with PageSpeed Insights I notice that this URL doesn't have the https href="http://aboutcookies.org"
Do you think it make sense to update it from http to https?

Also that Link contain no descriptive text. With descriptive link text, search engines can better understand your content.
Do you think it may be a good idea to change this?

thanks for the great banner =)

magnific-popup integration

Hi,
thanks for this nice utilty.

I'm using cookie-banner in a project where I would like to show the privacy policy in a popup managed by magnific-popup, but the "moreinfo" option support only links.

Any suggestion?

Could you help add git tag

Hello @zytzagoo I am the member of cdnjs project. We want to host this library. Git tag can help us to know your release version, and we can auto-update the version when you have new release. Could you please help us add git tag. Thanks for your help!

cdnjs/cdnjs#9956

Script block above the fold on Google PageSpeed Insights

Hi,

thank you for the amazing cookie banner script. I added the js file just before /body tag in this way, and it works very well. I use Wordpress.

<script type="text/javascript" id="cookiebanner" src=".../cbm.js"></script>

Google PageSpeed Insights says that the script is blocking the rendering of above-the-fold ( http://i.imgur.com/z7njWxo.png ). I tried to load in async mode, but in this way the banner doesn't appear.

How can i resolve this problem?

Thanks. :)

EU Cookie Law changes in 2018

In March 2018 a new EU cookie law is rolled out that a website visitor/user need to opt-in for the use of cookies and not just getting informed that cookies are used on a website. In case a user is not allowing (for whatever reasons) cookies than all scripts which use cookies on the site should be disabled.

I was wondering if the script could be adjusted to this new regulations. A website which has this functionality already is https://cookieconsent.insites.com/download/ (Point 5. ) but I really love this script here and would us it in the future as well.

How could this be achieved and how difficult would this be to be implemented?

moreinfo not working

Hi there,

I'm trying to use moreinfo parameter but it's not working. Others like data-message or data-linkmsg are working fine. Any clue?

Thanks in advance

`expires` option doesn't work with Firefox.

Hi,
I'm not setting the expires option because the default value Infinity is ok in my case.

I noticed that the cookie-banner shows from time to time after having agreed once, so I decided to set the expires option.

Setting the option I noticed that the expiration date is always one week (I tried to set it using seconds and also explicit Date object).

Initially I though it was a problem of the option, but I tested the same code with Chrome and it works well, so it's a Firefox (v.106) issue.

Padding or margin

Is it possible to add a padding or margin so the scrollbar does not cover up the banner in internet explorer?

banner disappears on page reload

Love this solution, simple, straight forward and seems to work well.

However, I found what I think is a bug.
If you load the page it comes up perfectly, but if I then do a refresh on the page, the banner does not load, not certain if from a regulatory point of view this would meet the requirements.
I changed the code to set the "true" option for wait as below, but still same behaviour:
new CookiesEuBanner(function(){
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    // Don't forget to put your own UA-XXXXXXXX-X code
    ga('create', '<%= trackingID %>', 'auto');
    ga('require', 'linkid', 'linkid.js');
    ga('send', 'pageview');
}), true;

Of course, if it is that regulations wise, its ok that it disappears after refresh I am happy to keep it the way it currently is.

Create bower package

What about creating a bower package so users can include and host the file themselves along their project?

Charset + Force into foreground

Is there a possibility to force UTF-8? (characters like ü ä ö are not displayed correctly).
Also, is there a way to force the cookie banner into the foreground? On my mobile page the banner is sometimes covered by content when scrolling.

Does not show up in Firefox or Chrome

It seems as the Cookie Banner does not show up in Firefox or Chrome (on Mac) anymore, neither in Safari on iOS. No "cookiebar: CookieAllowed" Cookie is set so far. Maybe you can take a look at that.

Attribution

Hi - is there a recommendation for how to include the copyright/licensing for this in source? I'm incorporating it into a fork of Open EdX that my company is using, and I want to make sure we give credit where it's due. The copyright on your readme.md suggests that it "shall be included in all copies or substantial portions of the Software."

Yet it isn't included in the .js file. Should it perhaps be added there?

Two links in the message possibility

I need two links, tried it like you can see here below but that doesn't work. Any idea how to fix this?

data-message="We use cookies ... and our <a href'/privacy'>privacy statement</a> and "
data-linkmsg="cookiestatement"
data-moreinfo="/cookies"
data-moreinfo-target="_parent">

Document min-height option

The data-min-height option should be documented in README.md.
I would have done it via a pull-request, but I just don't understand the point of this option...
Thanks :)

Multi-line data-message

Hi,
It would be great to have the possibility to add multi-line messages in the data-message field, e.g.
Lorem ipsum\ndolor sit amet"
Thanks!

Cookie Expire Date

Hi. I'm trying to customize the expire setting and set an expiration date on the cookie. How do I do this using the data-expires attribute? What value should it be if I for example would like the cookie to expire after 3 months. Should it be the time in milliseconds from now, or something like that?

acceptOnFirstVisit: true

acceptOnFirstVisit: true is not working for me. I'm using this implementation:

var options = {
    message: Locales[Laravel.language].layout.cookies.message,
    moreinfo: Locales[Laravel.language].layout.cookies.moreinfo,
    linkmsg: Locales[Laravel.language].layout.cookies.linkmsg,
    acceptOnFirstVisit: true
};
var cb = new Cookiebanner(options); cb.run();

How I can achieve that behavour??

Issue in Running Script IE 8

Hi this is a great script and works fine on all browsers apart from ie8. When i run the script in IE8 including the demo provided it does not display and I just get a Javascript error. Using the Dev tool in IE 10 and setting to IE8 mode I then get this error - SCRIPT438: Object doesn't support property or method 'hasOwnProperty' . Any help on this would be great

Custom close icon

Are you planning to add a possibility to change the default close icon (I see the icon is currently hardcoded)?

Allow hide "More Info" link

It's possible to add an option to hide the "More Info" link?

For example: moreinfo-hide - Hide more info link (default: false)

Suggestion: allow "accept-on-navigation"

The current implementation for accept-on-click can result a bit too restrictive, a softened solution would be to implement an accept-on-navigation which would fire whenever you click on a link or button element (excluding the cookie banner "Learn more" link).

Expiration

How can I set the inspiration up to 24 hours?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.