• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

203
Views
Pregunta de Javascript: no se puede agregar eventlistener para el inicio de carga del evento en el cuerpo de la página html

Estoy tratando de agregar eventlistener para event loadstart en el cuerpo de la página html pero no funciona.

Código HTML:

 <!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>

Código Javascript (rotar_texto.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

¿Por qué no funciona addEventListener para el inicio de carga de eventos en el cuerpo? ¿Estoy haciendo algo mal?

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El problema es que llamas a populate_body() inmediatamente cuando lo asignas, por eso funcionó :)

 body.onloadstart = populate_body();
almost 3 years ago · Juan Pablo Isaza Report

0

El evento loadStart ocurre cuando el navegador comienza a buscar los medios especificados y no se puede aplicar en el elemento del cuerpo.

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

Esta línea de código no agrega un detector de eventos, muestra la alerta porque ha llamado a la función populate_body().

No estoy seguro, pero tal vez el evento de carga sea lo que está buscando.

 function myFunction() { alert('body element loaded') } document.body.onload = myFunction;
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error