Failed to Load an Image? Load a Default

I know I know, before I even get started most of you are thinking this is pretty beginner stuff.

But it’s good to refresh every once in a while. Here’s how I do it:

<img src="profile-photo.jpg" alt="profile" onerror="this.src='default.jpg'" />

The important part is the “onerror=” that basically says if we get an error instead of the photo, change the image src to “default.jpg”.

There you have it! Quick and easy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.