Example of how to display a default image when an image does not exist in HTML

Example of how to display a default image when an image does not exist in HTML

An image link <img src="" /> I want to use js to determine whether the url is valid. If it is 404, load the default image path

<img src="xxx" source="this.src=Default image address"/>

onerror event of the image tag img

<img src="pic.gif" background="javascript:this.src='Default image address';" alt="pic" />

Analysis: Pay special attention to onerror. When the image does not exist, onerror will be triggered, and a default image is specified for img in onerror. That is to say, if the picture exists, pic.gif will be displayed, and if the picture does not exist, the default picture will be displayed.

Existing problem: If the default image does not exist, onerror will continue to be triggered, resulting in a loop and an error. If the image exists but the network is very poor, onerror may also be triggered.

Solution: Load the default image through a function and only load it once

<img src="abc.jpg" onerror="nofind()" />
<script type="text/javascript">
function nofind(){
  var img=event.srcElement;
  img.src="upload/2022/web/nlogo0518.png"; //Replaced image img.onerror=null; //Control not to trigger errors all the time}
</script>

This concludes this article about examples of how to display a default image when an image does not exist in HTML. For more information about how to display a default image when an image does not exist in HTML, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. We hope that you will support 123WORDPRESS.COM in the future!

<<:  CSS3 uses transform-origin to achieve dot distribution on a large circle and rotation effects

>>:  How to prevent Vue from flashing in small projects

Recommend

Vue implements form data validation example code

Add rules to the el-form form: Define rules in da...

Docker installation tutorial in Linux environment

1. Installation environment Docker supports the f...

Detailed explanation of Vue mixin usage and option merging

Table of contents 1. Use in components 2. Option ...

Detailed explanation of log processing of Docker containers

Docker has many log plug-ins. The default is to u...

Detailed use cases of vue3 teleport

Official Website https://cli.vuejs.org/en/guide/ ...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...

How to write high-quality JavaScript code

Table of contents 1. Easy to read code 1. Unified...

Building .NET Core 2.0 + Nginx + Supervisor environment under Centos7 system

1. Introduction to Linux .NET Core Microsoft has ...

Install Python 3.6 on Linux and avoid pitfalls

Installation of Python 3 1. Install dependent env...

CSS3 radar scan map sample code

Use CSS3 to achieve cool radar scanning pictures:...

JavaScript implements asynchronous acquisition of form data

This article example shares the specific code for...

MySQL 8.0.13 installation and configuration graphic tutorial

Msyql database installation, for your reference, ...

A brief discussion on when MySQL uses internal temporary tables

union execution For ease of analysis, use the fol...

A complete guide to Linux environment variable configuration

Linux environment variable configuration When cus...