📅  最后修改于: 2023-12-03 15:33:47.377000             🧑  作者: Mango
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.
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>
var cookieConsent = new CookieConsent();
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'
});
cookieConsent.init();
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 cookiesonDecline
: a function that is called when the user declines cookiesonInit
: a function that is called when the library is initializedonReset
: a function that is called when the user resets their choiceYou 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'); }
});
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.