Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda