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

198
Vistas
Preload and defer JS and call functions therein within the HTML doc

Suppose there's a JS file:

// a.js
function a(){ console.log('hello from a.js') }

and an HTML file where I want to preload and defer the a.js JS file such that I can call functions in a.js from within b.html:

<!-- b.html -->
<html>
<head>
  <link re='preload' as='script' href='a.js'>
</head>

<body>
</body>

<script defer src='a.js'>  <!-- calling a() below doesn't work -->
<script       src='a.js'>  <!-- calling a() below does    work -->

<script>
a()  // doesn't work with `defer`, works without `defer`
</script>
</html

How can I accomplish that?

One way is to wait for the page to load:

<script defer src='a.js'>
<script>
window.addEventListener('load', function(){ a() })  // Works
</script>

but this is rather inelegant. Is there a better way?

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