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

152
Visualizações
Is it possible to create script tag without setting a scr?

I have a Javascript code in the string which I want to append to the HTML body. And in that code I want to import some functions from other localization. This is how I am appending Javascript code to the HTML body:

const script = document.createElement('script');
script.setAttribute('type', 'module');
script.text = await view.addScript(); // that function is returning my Javascript code
document.body.appendChild(script);

When I do this I am getting an error: "Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec."

Which I understand. And when I am using appending code other way:

changing:

script.setAttribute('type', 'module');

to

script.setAttribute('type', 'text/javascript');

then I am getting and error: "Cannot use import statement outside a module" which I understand too.

My question is: is it possible to set module Javascript, where code is appended from a string, without setting a src attribute because in my case that src does not exists?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok, I solved the problem. When I code this way:

const script = document.createElement('script');
script.setAttribute('type', 'module');
script.text = await view.addScript(); // that function is returning my Javascript code
document.body.appendChild(script);

What was wrong? My script text included import statement. When a path in import is incorrect then you will get a MIME error. When I gave the correct path, the error was gone and now everything is working :)

about 4 years ago · Juan Pablo Isaza Relatório
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