wide-cartoon-5349
09/21/2024, 4:27 AMorange-terabyte-33302
09/21/2024, 4:27 AMorange-terabyte-33302
09/21/2024, 4:28 AMjavascript <script> // Wait for the GrowthBook SDK to load before running window.growthbook_queue = window.growthbook_queue || []; window.growthbook_queue.push((gb) => { // Function that uses feature flags to make changes to the page const applyFeatureFlags = () => { if(gb.isOn("dark-mode")) { document.documentElement.classList.add("dark"); } else { document.documentElement.classList.remove("dark"); } } // Call your function initially plus whenever new data is received applyFeatureFlags(); document.addEventListener("growthbookdata", applyFeatureFlags) }); </script>
This script waits for the GrowthBook SDK to load before running. It then uses a feature flag to add or remove a class from the document element based on whether the feature is on or off. The function is called initially and whenever new data is received. However, it's important to note that injecting JavaScript through the visual editor or any other way without dev work is currently not supported. It's also mentioned that injecting JavaScript strings as the value of a feature and evaluating it on the client is generally not recommended due to potential security risks [[source](https://linen.growthbook.io/t/8042956/hey-all-i-have-been-exploring-growthbook-for-couple-of-days-)]. For more complex changes, GrowthBook is working on a feature to allow JS and CSS injection via the visual editor, but this feature is not yet available [[source](https://linen.growthbook.io/t/9714304/hiya-i-m-trying-to-get-growthbook-to-send-experiment-data-to)].