Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda