Consentful
Guides

JavaScript API

Control the Consentful banner from your own code — open preferences, read or change consent, and listen for changes.

Consentful exposes window.CookieConsent once it has loaded.

MethodWhat it does
CookieConsent.getState()The visitor's current choice, or null if they haven't chosen.
CookieConsent.acceptAll()Allow every category.
CookieConsent.rejectAll()Allow only strictly necessary.
CookieConsent.accept(['analytics'])Allow exactly these categories (plus necessary).
CookieConsent.openPreferences()Open the preference center.
CookieConsent.withdraw()Forget the choice; the banner shows again.
CookieConsent.downloadReceipt()Download the visitor's consent receipt.

Examples

A "Cookie settings" link in your footer:

<a href="#" onclick="window.CookieConsent?.openPreferences(); return false;">Cookie settings</a>

Run code when consent changes:

window.addEventListener('cookieconsent:change', (event) => {
  const state = event.detail;
  if (state.categories.analytics) {
    // analytics allowed
  }
});

On this page

JavaScript API — Docs | Consentful