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

179
Visualizações
Can I load a Vanilla JS module from an HTML requested file?

I am trying to build a small, modular Vanilla SPA JS project and I am using JS modules for this. The structure of my project is as follows:

/index.html
/index.js
  /js
    /mypage.js
  /pages
    /mypage.html

mypage.html

<head>
    <script src="../js/mypage.js" type="module"></script>
</head>
<div>whatever content here</div>

mypage.js

console.log('mypage module has loaded')

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="./index.js" type="module"></script>
</head>
<body>
  <div id="app">
  </div>
</body>
</html>

index.js loads mypage.html through a fetch request, then sets the content of the #app div like so:

const content = await fetch(/pages/mypage.html).then(data => data.text())
document.getElementById('app').innerHTML = content

I feel like this is a stupid question, but why won't the mypage.js module be requested? Is it because modules should be bundled only once at load time because that is how the browser works, duh? (The content of mypage.html gets injected in the #app div, the mypage.js script however is not loaded, it does not appear in the network tab)
Thank you!
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