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

Several ways to switch between Vue Tab and cache pages

Table of contents 1. How to switch 2. Dynamically...

About input file control and beautification

When uploading on some websites, after clicking t...

More than 100 lines of code to implement react drag hooks

Preface The source code is only more than 100 lin...

Solve the problem of PhPStudy MySQL startup failure under Windows system

Report an error The Apache\Nginx service started ...

JavaScript Canvas draws dynamic wireframe effect

This article shares the specific code of JavaScri...

How to wrap HTML title attribute

When I was writing a program a few days ago, I wan...

Native JavaScript to achieve the effect of carousel

This article shares the specific code for JavaScr...

Detailed explanation of Vue's monitoring properties

Table of contents Vue monitor properties What is ...

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

The problem of form elements and prompt text not being aligned

Recent projects involve the creation of a lot of ...

Detailed steps to configure MySQL remote connection under Alibaba Cloud

Preface As we all know, by default, the MySQL ins...

MySQL learning database backup detailed explanation

Table of contents 1.DB,DBMS,SQL 2. Characteristic...

Solution for importing more data from MySQL into Hive

Original derivative command: bin/sqoop import -co...