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

147
Visualizações
add external html with javascript code without using iframe

I'm trying to load some html and javascript from one website without using iframe. I can render the html, css and it seems that the javascript files are being loaded, but I can't call functions from the loaded content.

I'm trying to load using fetch:

window.onload = function () {
    fetch(myURL)
        .then(function (response) {
            return response.text();
        })
        .then(function (body) {
            var dv = document.createElement('div');
            dv.innerHTML = body;
            document.body.appendChild(dv);
            //trying to call function here after appending the html content to my page
        });
}();

Is it possible, or it's blocked by browsers for security reasons?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you have to explicitly add a script element.
so what works for sure is:

 let text = "<h1>Test</h1><script>const callMe = () => { alert('called'); }" + "&lt;/script>";
    const scriptStart = text.indexOf('<script>');
    const scriptEnd = text.indexOf('&lt;/script>');
    const script = text.substring(scriptStart + "<script>".length, scriptEnd);
    text = text.substring(0, scriptStart) + text.substring(scriptEnd + "&lt;/script>".length);
    element.innerHTML = text;
    const scriptElement = document.createElement("script");
    scriptElement.innerHTML = script;
    document.head.appendChild(scriptElement);
    callMe();
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