JavaScript API
For developers who want to react to choices or open the banner.
window.cmp is available once the cmp:ready event has fired.
| Call | What it does |
|---|---|
cmp.show() |
Opens the banner. cmp.show(2) opens the settings. |
cmp.hide() |
Closes the banner. |
cmp.getConsent() |
Returns the current choices, or null if the visitor hasn’t chosen yet. |
cmp.getConsentId() |
Returns the visitor’s anonymous record ID. |
cmp.setConsent({ analytics: true }) |
Sets the categories you pass. |
cmp.acceptAll() / cmp.rejectAll() |
Does the same as the banner’s buttons. |
cmp.withdraw() |
Clears the stored choice and rejects everything. |
cmp.runScripts() |
Checks text/plain scripts again, for use after client-side navigation. |
cmp.state() |
Returns which rules this visitor got: { regime, place, rulebook }. |
cmp.on('consent', fn) |
Calls fn with { choice, id, regime, place, rulebook } every time the visitor decides. Returns a function that unsubscribes. |
Events on document: cmp:ready, with { regime, country, place, rulebook, lang }, and cmp:consent, with the same details as cmp.on('consent').
Attributes: data-cmp="settings" on any element makes it open the settings, data-cmp="ignore" exempts a script from blocking, and data-cmp-src on an iframe holds it back until its category is allowed.