On this page
Before you start
Use this for code of your own that sets cookies or sends data, such as a chat widget, a video player or a script written into your page. Auto-blocking already holds back scripts from websites your scan found. The JavaScript API covers what it can't, and works when Auto-blocking is off.
You need the CookieScan script on your website, and access to edit your website's code. The JavaScript API works on every plan.
Turn on the JavaScript API
Open the banner settings
Go to Cookie Banner, then Settings.
Tick the API setting
Tick Javascript API.
Save your settings
Select Save & Publish.
Pick the category
Check which category the script's cookies are in. Open Cookie Manager and find the cookies under Necessary, Preference, Statistics, Marketing or Unclassified. Your code checks the same category. See Review and categorise cookies.
Load the script after consent
Wrap the script in a function
Move the code that loads your script into a function, so it runs only when called.
Call it for returning visitors
Check
cookieScan.consentfor the category, and call the function if it istrue.Call it when a visitor accepts
Listen for the
cookiescan_consent_updateevent, and call the function if the category is accepted.
This example loads a chat widget once the visitor accepts Preference. Put it after the CookieScan script tag, and use your widget's own address.
<script src="https://banner.cookiescan.com/js?id=YOUR-COOKIESCAN-ID"></script>
<script>
function loadChatWidget() {
if (window.chatWidgetLoaded) return;
window.chatWidgetLoaded = true;
var s = document.createElement('script');
s.src = 'https://chat.example.com/widget.js';
document.head.appendChild(s);
}
// A returning visitor who has already accepted Preference
if (window.cookieScan && cookieScan.consent && cookieScan.consent.preference) {
loadChatWidget();
}
// A visitor who accepts Preference on this page
document.addEventListener('cookiescan_consent_update', function (e) {
if (e.detail.preference) loadChatWidget();
});
</script>The event fires only when Seamless consent (no page reload) is on. Otherwise the page reloads after the choice, and the check for returning visitors loads the widget. The same pattern works for a video or any other embed. JavaScript API lists every value you can check.
Check it worked
Open your website in a private window
Open your browser's network tools before the page loads.
Check before you choose
Your browser doesn't request your script while the banner waits for a choice.
Accept the category
Accept the category on your banner. Your script loads, after the reload if the page reloads.