Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Preguntas sobre el manejo de errores de eventos DOM

Leí este artículo de Ben Nadel sobre cómo aprovechar la fase de captura para capturar errores del evento de onload del elemento img , ya que mencionó que el evento de error no aparece.

Entonces lo que hizo fue

 refs.body.addEventListener( "error", handleErrorCapture, // According to the DOM (Document Object Model) specification, the ERROR and // LOAD events for Images do not BUBBLE up through the DOM tree. As such, we // have to use the CAPTURE phase, which starts at the top of the DOM and // descends down into the DOM toward the target element. true );

Mi primera pregunta es, ¿por qué no consideró usar window.onerror para capturar los errores? Parece que sería mucho más fácil.

Luego probé el mismo enfoque con un botón que, cuando se hacía clic más de 5 veces, arrojaba un error en su controlador onclick . Y luego adjunté un detector de eventos de error en document.body para capturar los eventos en la fase de captura de la propagación de eventos, tal como lo hizo el artículo.

 const btn = document.querySelector("#btn"); btn.addEventListener("click", () => { btn.textContent = Number(btn.textContent) + 1; if (Number(btn.textContent) > 5) { throw new Error("lol"); // 💣 } }); document.body.addEventListener( "error", function (event) { // 👇 this doesn't work console.log("Got an uncaught error: in body", event.error); }, true );

Sin embargo, no parece ser capaz de capturar los eventos de error. Entonces mi segunda pregunta es ¿por qué esto no funciona?

Finalmente, si adjunté un detector de error en window , entonces estaría funcionando.

Puede probar la demostración aquí https://codesandbox.io/s/dom-error-handling-oz66c?file=/src/index.js:254-424

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!