Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda