php - WordPress load javascript file if something... (after the page is loaded)

admin2025-01-07  4

I'm having an issue with making a website GDPR ready. It is loading a javascript file for InstraGram that is setting a cookie.

I was able to locate where this file is loaded in the plug-in directory but I dont want to edit this file because after that I cant update anymore.

So I was able to dequeue the script so it wont be loaded.

After that I tried everything to load the file conditionaly after the page is loaded. So when you accept the Cookies then the script wil be loaded.

This is the if statement and last thing I tried:

<script type="text/javascript">
    function CookiebotCallback_OnAccept() {
        if (Cookiebot.consent.marketing)
        {
        $('head').append('<script type="text/javascript" src="/wp-content/plugins/instagram-feed/js/sb-instagram.min.js"><\/script>');
        };
}
</script>

But I am starting to wonder more and more whether it is possible to load afterwards?

Somebody has done something like this before?

I'm having an issue with making a website GDPR ready. It is loading a javascript file for InstraGram that is setting a cookie.

I was able to locate where this file is loaded in the plug-in directory but I dont want to edit this file because after that I cant update anymore.

So I was able to dequeue the script so it wont be loaded.

After that I tried everything to load the file conditionaly after the page is loaded. So when you accept the Cookies then the script wil be loaded.

This is the if statement and last thing I tried:

<script type="text/javascript">
    function CookiebotCallback_OnAccept() {
        if (Cookiebot.consent.marketing)
        {
        $('head').append('<script type="text/javascript" src="/wp-content/plugins/instagram-feed/js/sb-instagram.min.js"><\/script>');
        };
}
</script>

But I am starting to wonder more and more whether it is possible to load afterwards?

Somebody has done something like this before?

Share Improve this question asked May 23, 2018 at 8:00 user2812779user2812779 1472 silver badges9 bronze badges 3
  • It would depend on how the original JS was built. If it's set to trigger at a certain point that has already passed, nothing will happen. Regardless this is really a JS question, so you might be better off asking at stackoverflow.com – Jacob Peattie Commented May 23, 2018 at 8:17
  • You should probably also raise this with the developer of the plugin, as something they might be able to address. – Jacob Peattie Commented May 23, 2018 at 8:18
  • I'd hope whatever cookie plugin would handle this: add a new after-cookies-accepted scripts queue, generate your cookies accepted block themselves, and provide functions to move scripts over to that queue from the regular queue. – Rup Commented Apr 7, 2024 at 22:34
Add a comment  | 

1 Answer 1

Reset to default 0

try now this code

<script type="text/javascript">
function CookiebotCallback_OnAccept() 
{
    if (Cookiebot.consent.marketing)
    {
        var script = document.createElement('script');
        script.src = "/wp-content/plugins/instagram-feed/js/sb-instagram.min.js";
        document.body.appendChild(script);
    }
}

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262719a835.html

最新回复(0)