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

267
Visualizações
What is the alternative to document.write to include js files

I would like to collect all my javscript links to one javascript and only include this to all my html files.

I got it working with:

document.write('<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js" ></script>');
...
...

But I get warnings: A parser-blocking, cross site (i.e. different eTLD+1) script, , is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See for more details.

What are the alternatives to document.write? I have been seraching but I can not find a solution.

Edit

I have a small example:

Main file

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>

<!-- No error 
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script> -->

<!-- Error -->
<script type="text/javascript" src="test.js"></script> 

<script>
$(function() {
    console.log("SCRIPT FUNCTION");
}); 

</script>

</html>

test.js

var x = document.createElement('script');
x.src = 'https://code.jquery.com/jquery-3.5.1.js';
document.getElementsByTagName("head")[0].appendChild(x);

With the jquery script declared in the html file in works by when I try to add the jquery file in test.js I get the error: test.html:20 Uncaught ReferenceError: $ is not defined

What is wrong with the code when I try to add the js file instead of declare it in the html file?

With document.write the js files are loaded direct? And with append they will not load until later?

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