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

203
Visualizações
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 à 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