Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Execute injected script element after it has been loaded dynamically

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>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!