Skip to content

Instantly share code, notes, and snippets.

@hungtcs
Last active October 26, 2023 09:36
Show Gist options
  • Save hungtcs/5734cf5b8ebe55a3b758c21c93df75e0 to your computer and use it in GitHub Desktop.
Save hungtcs/5734cf5b8ebe55a3b758c21c93df75e0 to your computer and use it in GitHub Desktop.
Show fallback image when image loading fails
<object data="path/to/image-00.png" type="image/png">
<img src="path/to/fallback.png" />
</object>
<!-- why not this -->
<img src="path/to/image-00.png" onerror="this.src = 'path/to/fallback.png'" />
<!-- 1. object is a pure html solution and does not require js. -->
<!-- 2. if the backup image fails to load, there is a risk of an infinite loop -->
<!-- 3. however, the object tag has a larger overhead -->
<!-- 4. maybe I should create a web component that supports regression to replace the above -->
<!-- and -->
<img src="path/to/image-00.png" onerror="this.onerror = null; this.src = 'path/to/fallback.png'" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment