Docs

JavaScript API

AdminUpdated Sep 15, 2026

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: consent is a getter property, not a method — read it as CookieMunch.consent, not CookieMunch.consent(). This matches the Cookiebot API shape.

Properties

Property

Type

Description

consent

object

Current per-category consent snapshot: { necessary, preferences, statistics, marketing, stamp, ...customCategories }. stamp is the consent-receipt id — see consent log, stats & export.

consented

boolean

true once the visitor has an active, valid consent decision (stored or just made).

declined

boolean

true when the visitor explicitly declined everything.

hasResponse

boolean

true once any decision — explicit or implied — has been recorded for this visitor.

doNotTrack

boolean

Mirrors navigator.doNotTrack === '1' at load time.

regulations

object

The resolved regulation set applicable to the visitor (region-dependent).

Methods

Method

Description

show()

Re-open the banner (first layer).

hide()

Hide the banner without changing consent.

renew()

Re-prompt the visitor — identical to show(), used semantically when re-consent is required (e.g. after a policy version bump).

showSettings()

Open the granular preference center (second layer / modal). Wire this to your footer "Cookie settings" link.

withdraw()

Revoke all non-necessary consent and re-block previously activated tags.

submitCustomConsent(preferences, statistics, marketing)

Programmatically submit a specific per-category decision, bypassing the UI.

runScripts()

Force-run any scripts already unblocked by the current consent state (rarely needed — the engine does this automatically on every decision).

getScript(url, async, callback?)

Inject a <script src="url"> tag directly (helper for code that doesn't want to manage the DOM itself). Does not check consent — call it only after you've confirmed the relevant category yourself.

onConsentChange(callback)

Subscribe to consent changes. Fires on explicit accept/decline and on returning-visitor hydration of stored consent. Returns an unsubscribe function.

goToView(id)

Jump to a named view inside a flow-based (Banner Studio v2) banner. No-op on v1 banners.

getActiveView()

Returns the current view id in a v2 flow banner, or null (v1 banners, or before the banner is mounted).

consentId()

Convenience for CookieMunch.consent.stamp — the receipt id, useful for self-serve erasure/export flows run from the visitor's own page.

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

CookieMunchOnLoad / CookiebotOnLoad

The embed has finished bootstrapping.

CookieMunchOnDialogInit / CookiebotOnDialogInit

The banner is about to be shown for the first time.

CookieMunchOnDialogDisplay / CookiebotOnDialogDisplay

The banner (or preferences view) becomes visible — including re-opens via show()/showSettings().

CookieMunchOnAccept / CookiebotOnAccept

The visitor accepted (all or some categories).

CookieMunchOnDecline / CookiebotOnDecline

The visitor declined.

CookieMunchOnConsentReady / CookiebotOnConsentReady

A valid consent state exists — fires for fresh decisions and for returning visitors with stored consent.

CookieMunchOnTagsExecuted / CookiebotOnTagsExecuted

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.

Was this page helpful?
JavaScript API