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

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?

Topic cookies functions php jquery Wordpress javascript

Category Web


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);
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.