Skip to content

EARN UP TO 50% COMMISSION WITH OUR RESELLER ACCOUNTS + REGULAR PASSIVE INCOME SIGN UP TODAY

Install CookieScan

Load your own scripts after consent

Hold back a script, widget or video of your own until the visitor accepts its cookie category, using the CookieScan JavaScript API.

Updated 3 min read

Applies to:
  • Cookie banner

https://cookiescan.com/help/load-your-own-scripts-after-consent/ Β· Updated 12 October 2026

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

  1. Open the banner settings

    Go to Cookie Banner, then Settings.

  2. Tick the API setting

    Tick Javascript API.

  3. 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.

  1. Wrap the script in a function

    Move the code that loads your script into a function, so it runs only when called.

  2. Call it for returning visitors

    Check cookieScan.consent for the category, and call the function if it is true.

  3. Call it when a visitor accepts

    Listen for the cookiescan_consent_update event, 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.

HTML
<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

  1. Open your website in a private window

    Open your browser's network tools before the page loads.

  2. Check before you choose

    Your browser doesn't request your script while the banner waits for a choice.

  3. Accept the category

    Accept the category on your banner. Your script loads, after the reload if the page reloads.

Next steps

Was this article helpful?

Thanks for your feedback.

Please don't include personal details. We can't reply here.

Thanks for your feedback.

Please don't include personal details. We can't reply here.

Thanks, your comment has been sent.

Sorry, we couldn't record your feedback. Please try again later.

Still stuck?

Our support team can help with anything these guides don't cover. Tell us your CookieScan ID and the page address so we can look straight away.

Back to Install CookieScan