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

14 techniques for high-performance websites

Original : http://developer.yahoo.com/performance...

linux No space left on device 500 error caused by inode fullness

What is an inode? To understand inode, we must st...

How does Vue implement communication between components?

Table of contents 1. Communication between father...

How to use jconsole to monitor remote Tomcat services

What is JConsole JConsole was introduced in Java ...

Typescript+react to achieve simple drag and drop effects on mobile and PC

This article shares the specific code of typescri...

Implementation of Vue single file component

I recently read about vue. I found a single-file ...

Detailed explanation of how to configure static IP in Centos8

After installing centos 8, the following error wi...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

960 Grid System Basic Principles and Usage

Of course, there are many people who hold the oppo...

How to implement MySQL master-slave replication based on Docker

Preface MySQL master-slave replication is the bas...

Javascript to achieve the effect of closing advertisements

Here is a case study on how to close ads using Ja...

Analysis of MySQL example DTID master-slave principle

Table of contents 1. Basic Concepts of GTID 2. GT...