I make an ajax request that injects a script node in my head or body element like this:
window.addEventListener("DOMContentLoaded", () => {
this._client.get('/getFragment', (responseText) => {
const obj = JSON.parse(responseText);
const scriptTag = document.createElement('template');
scriptTag.innerHTML = obj.fragment;
document.body.appendChild(scriptTag.content);
});
});
The issue now is that the script is injected into the DOM, but as this JS script calls another external URL I can see in the network monitor that the URL of the script is not called/executed. How can make sure that the script tag is executed after I injected it into the DOM?
The fragment that I would like to inject is this one:
<script id="mcjs">!function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,"script","https://chimpstatic.com/mcjs-connected/js/users/1f.js");</script>
I'm not quite sure I understand what you are asking, but I believe the the problem is that you are creating a template element, and browsers usually doesn't execute the content of those. Try using document.createElement('div') instead.