Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda