HTML fallback images on error

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

The other day I encountered some issues with images for external sources not loading.
I saved a link to someone's Twitter profile image in this case. When the user changes this image, it doesn't auto reflect the new one.
So I decided to look into fallback images. And it's surprisingly easy.
What we want to achieve:
Note: Alternatively, you could decide to remove the image
Let's set up a sample HTML experiment.
<img
src="https://images.pexels.com/photos/163822/color-umbrella-red-yellow-163822.jpeg"
/>
This will load an Photo by Pixabay from Pexels (a stock image site).
This will work perfectly, but now let's try and destroy it by randomly adding some numbers to the image.
<img
src="https://images.pexels.com/photos/163822/color-umbrella-red-yellow-00000.jpeg"
/>
With this, we get the super annoying broken image.

So what can we do when this happens?
We can use the onerror attribute on the image to set a fallback.
It works like this:
<img
src="https://images.pexels.com/photos/163822/color-umbrella-red-yellow-00000.jpeg"
onerror="this.onerror=null; this.src='https://images.pexels.com/photos/159868/lost-cat-tree-sign-fun-159868.jpeg'"
/>
We use the onerror to set the error to null and set the src of the image to a fallback.
In our case, a photo of a missing cat.
Note: It's not a great practice to rely on external images. You ideally want to have the fallback image locally in the file system, so it's a reliable fallback.
You can see it in action in the following CodePen. The first image loads, and the second one is broken.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter