Skip to main content

Dynamic loading of configurations via custom code

Adrian ESCRIVA avatar
Written by Adrian ESCRIVA
Updated yesterday

This article is part of the series Adapting your banner to each version of your site.

Before following the guide below, make sure you’ve read the overview article: Choose the right method to load the correct Axeptio banner.


Option 1: Based on the browser language

jsCopyEdit// Replace '000000000000000' with your project ID window.axeptioSettings = { clientId: '000000000000000', cookiesVersion: { type: "browser_locale", cases: { default: "-en", "fr-FR": "-fr", "it-IT": "-it", } } }; (function(d, s) { var t = d.getElementsByTagName(s)[0], e = d.createElement(s); e.async = true; e.src = "//static.axept.io/sdk.js"; t.parentNode.insertBefore(e, t); })(document, "script");

Option 2: Based on a custom function

For example, using the lang attribute of the HTML tag:

jsCopyEdit// Replace '000000000000000' with your project ID window.axeptioSettings = { clientId: '000000000000000', cookiesVersion: { type: "custom_function", config: { function: () => { return document.documentElement.lang; } }, cases: { default: "-en", "fr-FR": "-fr", "it-IT": "-it", } } }; (function(d, s) { var t = d.getElementsByTagName(s)[0], e = d.createElement(s); e.async = true; e.src = "//static.axept.io/sdk.js"; t.parentNode.insertBefore(e, t); })(document, "script");

The version names used in the cases above (e.g. -en, -fr, -it) correspond to configuration IDs you can find in your Axeptio dashboard.


Special case: SPAs or sites without full page reload

On modern websites like Single Page Applications (SPAs), changing the language doesn’t trigger a full page reload. In this case, the Axeptio banner may remain stuck in the original language.

To handle this, you can use the axeptioSDK.setCookiesVersion() function to update the widget dynamically without reloading the page.

This approach requires basic JavaScript knowledge and is mainly for developers. For most websites where changing the language reloads the page (e.g. via URL changes), this step is not necessary.

Wherever your JavaScript handles the language change, you just need to call:

jsCopyEditaxeptioSDK.setCookiesVersion('it-version');

Replace 'it-version' with the ID of the configuration you want to load.
For example, you might have these configurations: it-version, fr-version, en-version.

Capture_d_e_cran_2022-08-25_a__15.34.42.png
Did this answer your question?