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

118
Views
Problem with Include JavaScript from another JavaScript in the browser

I am importing in my main JS another JS from a CDN conditionally like this:

if (('standalone' in navigator) && (!navigator.standalone)) {
  import('https://unpkg.com/pwacompat');
}

But I'd like to self-host it and include this JS file into my main JS file with JavaScript in the browser method. Like this:

pwacompact.js:

function pwacompact() {
     console.log("pwacompact");
}
export { logpwacompact };
// here goes the js I want to import...

main.js:

// ... other functions preceding the conditional import statement, and then:
if (('standalone' in navigator) && (!navigator.standalone)) {
  import { pwacompact } from "pwacompact.min.js";
logpwacompact();
}

The problem in main.js is that I want to place this import at the bottom of the script, but I can't because I get a Parse error: The import statement may only appear at the top level.

Please, how can I solve this? How can I include the second JS into the main JS and load it conditionally?

Thanks!

about 4 years ago · Santiago Trujillo
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!