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

279
Vistas
Javascript question: Cannot add eventlistener for event loadstart on body of html page

I am trying to add eventlistener for event loadstart on body of html page but it is not working.

Html code:


<!doctype html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Rotate Text</title>
        <script src="rotate_text.js"></script>
    </head>
    <body style="background-color:powderblue;">
    </body>
</html>

Javascript code (rotate_text.js):


var body = document.getElementsByTagName("body")[0];

function populate_body() {
    alert("Called");
} // end of populate_body

// body.onloadstart = populate_body(); // ====> THIS WORKS
body.addEventListener("loadstart", populate_body); // ====> THIS DOESN'T WORK

Why is addEventListener for event loadstart on body not working? Am I doing something wrong?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is that you call populate_body() immediately when you assign it, that's why it worked :)

body.onloadstart = populate_body();
about 4 years ago · Juan Pablo Isaza Denunciar

0

The loadStart event occurs when the browser starts looking for the specified media and it can not applied on body element.

// body.onloadstart = populate_body(); // ====> THIS WORKS

This line of code doesn't add event listener, it shows the alert because you have called populate_body() function.

I'm not sure but maybe onload event is what is you are looking for.

function myFunction() {
 alert('body element loaded')
}
document.body.onload = myFunction;
about 4 years ago · Juan Pablo Isaza Denunciar
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