Prompt
block scripts until cookie consent
Latest observation
Oct 29, 2026
OpenAI APIWeb search: off
To block scripts until cookie consent, you generally want to:
- Render scripts in a non-executable type
- Only activate them after consent is granted
- Delay third-party tags with a consent manager or custom logic
Common approach
1) Mark scripts as “blocked”
Instead of:
<script src="https://example.com/analytics.js"></script>
use:
<script type="text/plain" data-consent="analytics" data-src="https://example.com/analytics.js"></script>
Or for inline scripts:
<script type="text/plain" data-consent="marketing">
console.log('marketing script');
</script>
These won’t execute automatically.
2) After consent, convert them to real scripts
When the user accepts the relevant category, replace them with executable <script> tags:
<script>
function activateConsentScripts(category) {
document.querySelectorAll(`script[type="text/plain"][data-consent="${category}"]`).forEach(oldScript => {
const newScript = document.createElement('script');
if (oldScript.src || oldScript.dataset.src) {
newScript.src = oldScript.src || oldScript.dataset.src;
} else {
newScript.text = oldScript.textContent;
}
// Copy attributes if needed
Array.from(oldScript.attributes).forEach(attr => {
if (!['type', 'data-consent', 'data-src'].includes(attr.name)) {
newScript.setAttribute(attr.name, attr.value);
}
});
oldScript.parentNode.replaceChild(newScript, oldScript);
});
}
</script>
Then call:
activateConsentScripts('analytics');
activateConsentScripts('marketing');
when consent is granted.
Better option: use a consent management platform
If you’re dealing with:
- Google Analytics / GTM
- Meta Pixel
- Hotjar
- other ad/marketing scripts
it’s usually easier and safer to use:
- Google Consent Mode
- Cookiebot
- OneTrust
- Complianz (WordPress)
- Iubenda
These tools handle:
- script blocking
- consent categories
- tag firing after consent
- region-specific compliance
Important notes
- Do not load third-party scripts before consent if they set cookies or track users.
- Essential scripts (site functionality, security, shopping cart) may be allowed without consent depending on your jurisdiction.
- If you use a framework like Next.js, React, WordPress, or GTM, the implementation may differ.
If you want, I can show you:
- a plain HTML/JS example
- a Google Tag Manager setup
- or how to do this in WordPress / Next.js / React