Reference
JavaScript API
Open the banner from your own button, read the visitor's choice and react when it changes.
Once the banner script has loaded, a CookieTrue object is available on the page.
#Methods
| Call | What it does |
|---|---|
CookieTrue.show() | Shows the banner |
CookieTrue.showSettings() | Opens the category settings |
CookieTrue.acceptAll() | Accepts every category |
CookieTrue.rejectAll() | Rejects every optional category |
CookieTrue.setConsent({ analytics: true }) | Sets chosen categories |
CookieTrue.onChange(fn) | Calls fn whenever the choice changes |
CookieTrue.consent | The current choice |
CookieTrue.region | The visitor's region |
CookieTrue.version | The script version |
CookieTrue.ready | true when the script has started |
#Open settings from a link
Any element with data-ct-open reopens the settings when clicked, and so does a link to #cookietrue-settings:
<a href="#cookietrue-settings">Change cookie settings</a>
<button data-ct-open>Cookie settings</button>#Events
document.addEventListener('cookietrue:ready', () => {
console.log(CookieTrue.consent);
});
document.addEventListener('cookietrue:consent', (event) => {
// runs every time the visitor decides
console.log(event.detail);
});#Run your own code after consent
CookieTrue.onChange((consent) => {
if (consent.analytics) startMyAnalytics();
});Still stuck? Write to [email protected]. A person answers, usually the same day.