A lightweight, dependency-free cookie consent script with a drop-in banner, a preference modal, consent change events, translations, and simple styling hooks.
Drop cookie-consent.js into your project, include it with a script tag, and add a cookie settings link so users can reopen preferences later.
<script src="/path/to/cookie-consent.js"></script><a href="#" id="open-cookie-settings">Cookie settings</a>
<script>
document.getElementById("open-cookie-settings").addEventListener("click", function(event) {
event.preventDefault();
if (window.CookieConsent && typeof window.CookieConsent.showSettings === "function") {
window.CookieConsent.showSettings();
}
});
</script>Minimal config:
<script>
window.CookieConsentConfig = {
cookieMaxAge: 15768000
};
</script>
<script src="/path/to/cookie-consent.js"></script>cookieMaxAgeConsent cookie lifetime in seconds. Default:15768000seconds, about six months.languageSets the default language key for the current page.cc_langoverrides it on page load.translationsDefines language blocks for banner, modal, accessibility, and category copy.textLegacy single-language override for banner, modal, accessibility, and category copy.audit.enabledStores consent audit records in local storage undercookie_consentwhentrue.debugModeDevelopment override for consent behavior.0= normal,1= allow all optional consent,2= deny all optional consent.
The script exposes window.CookieConsent:
getConsent()getChoices()getStatus()hasValidConsentBitmask()getConsentDebugMode()getLanguage()isCategoryEnabled(categoryName)isAnalyticsEnabled()isMarketingEnabled()isNecessaryEnabled()setLanguage(languageKey)showBanner(options)showSettings(options)reset()
Language can also be overridden in JavaScript with showBanner({ language }), showSettings({ language }), or setLanguage(languageKey).
The script dispatches cookieconsent:change whenever consent is applied or updated.
The script uses:
- Cookie:
consent_status - Cookie:
gpc_popup_shownfor the GPC acknowledgement popup - Local storage:
cookie_consentwhenaudit.enabledistrue
The consent cookie stores a 3-bit mask for necessary, analytics, and marketing.
Working examples live in examples/.
- Start with
examples/index.htmlfor the main example hub. - Use
examples/full-integration.htmlfor a complete reference implementation. - Use the feature pages for focused examples of translations, styling overrides, events, audit storage, GPC handling, debug mode, and public API usage.
This project supports common website consent flows and integration patterns. You are responsible for configuring it correctly for your site, tags, policies, and legal obligations.
- Intended for modern browsers
- Uses
crypto.randomUUID()when available and falls back when it is not - Uses
CustomEvent, DOM APIs, local storage, and cookies - Checks
navigator.globalPrivacyControlwhen available