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

160
Visualizações
Load JavaScript before html page

Im learning JS basics and I need to my html page loads before JS if its possible, I`ve tried

document.addEventListener("DOMContentLoaded", function(event) { code here });

But it didnt work, I also tried to put my "script" link in my html body but it didnt change anything, tried to type "defer" in "script", nothing happened.

JS code

var lbs = prompt("What is the weight in pounds (lbs)?")
var kg = pounds * 0.454
alert("That is: " + kg + " kilograms")
console.log("Conversion completed")

HTML code is just a body with simple Lorem text in p tag

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

0

Normally, you should add it in the bottom of the body

<html>
    <head>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>CRP</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <!-- content -->
        <script src="script.js"></script>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

DOMContentLoaded event should work. It will be triggered after whole DOM on page is loaded. Maybe try window load event. It will be triggered after the whole page with all references (images, etc.) will be loaded.

Debug all errors you have, like you are using lbs variable and then the pounds variable that is not initialized in your code.

window.addEventListener("load", (e) => {});
document.addEventListener("DOMContentLoaded", (e) => {});
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