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

186
Visualizações
Error when JS in <head> tried to click on doc element in <body> (How to defer parsing?)

I've got JS code in my <head> that I'd like to click an element in the <body>. This is causing errors even though I've tried to postpone running the JS by:

  • calling 'defer'
  • using 'document.onload'
  • using 'window.onload'
to postpone the execution.

Is there any way to have my JS in the <head> or do I just need to move it to the bottom of the <body>?

Here's a test page showing the issue:

  <head>
    <script defer>
      document.addEventListener(
        "DOMContentLoaded",
        document.getElementById("my_item").click()
      );
    </script>
  </head>
  <body>
    <h1 id="my_item">Testing Defer JS</h1>
    <script></script>
  </body>
</head>
about 4 years ago · Juan Pablo Isaza
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