JavaScript API
- Written for
- + Written for
- Deprecated
- + Deprecated
- Applies to
- + Applies to
JavaScript API
Once consent.js has loaded (see embed scripts), it exposes a global window.CookieMunch object (aliased as window.Cookiebot for migration compatibility — both point at the exact same object, so either name works everywhere below).
// Read the current consent state
const state = CookieMunch.consent;
// → { necessary: true, preferences: false, statistics: true,
// marketing: false, stamp: 'a1b2c3…' }
// React to changes
CookieMunch.onConsentChange((state) => {
if (state.statistics) loadAnalytics();
});
// Open the banner or the granular preferences view
CookieMunch.show();
CookieMunch.showSettings();
// Withdraw consent and re-block non-essential tags
CookieMunch.withdraw();Note:
consentis a getter property, not a method — read it asCookieMunch.consent, notCookieMunch.consent(). This matches the Cookiebot API shape.
Properties
Property | Type | Description |
|---|---|---|
|
| Current per-category consent snapshot: |
|
|
|
|
|
|
|
|
|
|
| Mirrors |
|
| The resolved regulation set applicable to the visitor (region-dependent). |
Methods
Method | Description |
|---|---|
| Re-open the banner (first layer). |
| Hide the banner without changing consent. |
| Re-prompt the visitor — identical to |
| Open the granular preference center (second layer / modal). Wire this to your footer "Cookie settings" link. |
| Revoke all non-necessary consent and re-block previously activated tags. |
| Programmatically submit a specific per-category decision, bypassing the UI. |
| Force-run any scripts already unblocked by the current consent state (rarely needed — the engine does this automatically on every decision). |
| Inject a |
| Subscribe to consent changes. Fires on explicit accept/decline and on returning-visitor hydration of stored consent. Returns an unsubscribe function. |
| Jump to a named view inside a flow-based (Banner Studio v2) banner. No-op on v1 banners. |
| Returns the current view id in a v2 flow banner, or |
| Convenience for |
onConsentChange semantics
const unsubscribe = CookieMunch.onConsentChange((state) => {
console.log('marketing granted:', state.marketing);
});
// later, if needed:
unsubscribe();The callback fires once per explicit user decision (accept-all, decline-all, or a granular save), and once more on page load for returning visitors whose stored consent is being restored. It does not fire on every internal lifecycle tick — only on genuine state changes — so it's safe to wire directly to tag-loading logic without de-duplicating yourself.
Lifecycle events
For code that prefers DOM events over the callback API (or migrating from Cookiebot), every lifecycle transition is also dispatched as a CustomEvent on window, under both the CookieMunch* and Cookiebot* names, with the consent state in event.detail:
Event | Fires when |
|---|---|
| The embed has finished bootstrapping. |
| The banner is about to be shown for the first time. |
| The banner (or preferences view) becomes visible — including re-opens via |
| The visitor accepted (all or some categories). |
| The visitor declined. |
| A valid consent state exists — fires for fresh decisions and for returning visitors with stored consent. |
| Previously blocked tags have finished being reactivated. |
window.addEventListener('CookieMunchOnAccept', (e) => {
console.log('consent state:', e.detail);
});Equivalently, you can define a global callback function named CookieMunchCallback_OnAccept (or the Cookiebot-prefixed variant) and it will be invoked directly — no addEventListener needed. This mirrors Cookiebot's callback convention exactly, for drop-in migration.
Declarative triggers (no JS required)
Any element with data-fc-open="banner" or data-fc-open="preferences" opens the corresponding layer on click, with no wiring needed:
<a href="#" data-fc-open="preferences">Cookie settings</a>
<a href="#" data-fc-open="banner">Manage cookies</a>data-cc="show-settings" is also recognized (Cookiebot-compatible alias for data-fc-open="preferences").
Next: script blocking & data-attributes for prior-blocking markup, or back to embed scripts for install-tag details.