Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!