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

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