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

197
Views
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
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!