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

199
Views
How to catch img 404s that respond with a placeholder?

Is it possible to listen to an error on an image if the server sends a "backup" image (while still responding with a 404 status code)? This particular incident is related to YouTube thumbnails. I know you can listen to the error event on the element, but that only seems to trigger if the request does not resolve into an image at all.

const fallbackImageUrl = 'https://via.placeholder.com/320x180';

const images = document.body.querySelectorAll('img');

for (const img of images) {
  img.addEventListener('error', function() {
    this.src = fallbackImageUrl;
  });
}
<p>This image correctly resolves the error:</p>
<img src="https://www.example.com/not-an-image.jpg" />

<p>This image is a 404, but the server sends a backup:</p>
<img src="https://i1.ytimg.com/vi/blahblah/mqdefault.jpg" />

A similar effect is achievable by sending a separate HTTP request to the image URL in order to check the status code, but I'm wondering if this is possible without another call.

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!