Callbacks and Events
There are a few callbacks/events at your disposal to handle different kinds of situations. Each callback has a corresponding custom event, named after the callback function itself.
Available callbacks:
onFirstConsentonConsentonChangeonModalShowonModalHideonModalReady
When the consent callbacks run:
| What happens | Callbacks, in this order |
|---|---|
| The user makes their first choice | onFirstConsent, onConsent |
| A page loads, and the consent is valid | onConsent |
| The user changes their choice later | onChange, only if a category or service changed |
| The revision changes, or the cookie expires | like a first choice |
In opt-out mode, the first choice also fires onChange if it differs from the categories that were enabled by default.
onFirstConsent
This event is triggered only the very first time that the user expresses their choice of consent (accept/reject).
Example
javascriptCookieConsent.run({ onFirstConsent: ({cookie}) => { // do something } });using event listener:
javascriptwindow.addEventListener('cc:onFirstConsent', ({detail}) => { // detail.cookie // do something });
onConsent
This event is triggered the first time the user makes their choice, like onFirstConsent, and also on every later page load.
Example
javascriptCookieConsent.run({ onConsent: ({cookie}) => { // do something } });using event listener:
javascriptwindow.addEventListener('cc:onConsent', ({detail}) => { // detail.cookie // do something });
onChange
This event is triggered when the user modifies their preferences and only if consent has already been provided.
Example
javascriptCookieConsent.run({ onChange: ({cookie, changedCategories, changedServices}) => { // do something } });using event listener:
javascriptwindow.addEventListener('cc:onChange', ({detail}) => { /** * detail.cookie * detail.changedCategories * detail.changedServices */ // do something });
onModalShow
This event is triggered when one of the modals is visible.
Example
javascriptCookieConsent.run({ onModalShow: ({modalName}) => { // do something } });using event listener:
javascriptwindow.addEventListener('cc:onModalShow', ({detail}) => { /** * detail.modalName */ // do something });
onModalHide
This event is triggered when one of the modals is hidden.
Example
javascriptCookieConsent.run({ onModalHide: ({modalName}) => { // do something } });using event listener:
javascriptwindow.addEventListener('cc:onModalHide', ({detail}) => { /** * detail.modalName */ // do something });
onModalReady
This event is triggered when a modal is created and appended to the DOM.
Example
javascriptCookieConsent.run({ onModalReady: ({modalName, modal}) => { // do something } });using event listener:
javascriptwindow.addEventListener('cc:onModalReady', ({detail}) => { /** * detail.modalName * detail.modal */ // do something });