📜  preview.cookie-consent.js (1)

📅  最后修改于: 2023-12-03 15:33:47.377000             🧑  作者: Mango

Introduction to preview.cookie-consent.js

preview.cookie-consent.js is a small JavaScript library that helps you comply with the EU cookie law. It displays a pop-up notification that informs the user about the use of cookies on your website and asks for their consent. The user can either accept or decline the use of cookies.

Features
  • Easy to install and use
  • Customizable pop-up design and content
  • Option to accept or decline cookies
  • Option to remember user's choice
  • Option to show pop-up only once or every time the website is accessed
  • Multilingual support
Installation

You can install preview.cookie-consent.js via npm or by downloading the source code:

npm install preview.cookie-consent.js

Alternatively, you can download the source code from GitHub and link to it in your HTML file:

<script src="path/to/preview.cookie-consent.js"></script>
Usage
  1. Create a new instance of CookieConsent:
var cookieConsent = new CookieConsent();
  1. Customize the pop-up design and content:
cookieConsent.setPopupDesign({
  backgroundColor: '#273248',
  textColor: '#fff',
  buttonColor: '#76b852',
  buttonTextColor: '#fff',
  position: 'bottom',
  padding: '20px'
});

cookieConsent.setPopupContent({
  title: 'Cookies on our website',
  message: 'We use cookies to give you the best experience on our website. By continuing to use our site, you agree to our use of cookies.',
  acceptButtonLabel: 'Accept',
  declineButtonLabel: 'Decline',
  moreInfoLabel: 'More information',
  moreInfoHref: 'https://example.com/cookies'
});
  1. Initialize the library:
cookieConsent.init();
Options

The following options are available:

  • cookieName: the name of the cookie that stores the user's choice (default: 'cookieConsent')
  • cookieDuration: the duration of the cookie (in days) (default: 365)
  • onAccept: a function that is called when the user accepts cookies
  • onDecline: a function that is called when the user declines cookies
  • onInit: a function that is called when the library is initialized
  • onReset: a function that is called when the user resets their choice

You can set these options when you create a new instance of CookieConsent:

var cookieConsent = new CookieConsent({
  cookieName: 'myCookie',
  cookieDuration: 30,
  onAccept: function() { console.log('Cookies accepted'); },
  onDecline: function() { console.log('Cookies declined'); },
  onInit: function() { console.log('Library initialized'); },
  onReset: function() { console.log('User choice reset'); }
});
Conclusion

preview.cookie-consent.js is a simple and effective way to comply with the EU cookie law. It is easy to install, customize and use. You can download it from npm or from GitHub and use it in your next project.