Home > Web Front-end > JS Tutorial > How to determine whether the image in the web page is loaded successfully or failed?

How to determine whether the image in the web page is loaded successfully or failed?

coldplay.xixi
Release: 2020-06-30 16:41:39
Original
4417 people have browsed it

How to determine whether the image in the web page is loaded successfully or failed: first you need to determine whether the image can be loaded successfully; then automatically change it to a system image such as "Unable to find image", the code is [for(var i=0;i

How to determine whether the image in the web page is loaded successfully or failed?


How to determine whether the image in the web page is loaded successfully or failed:

When there are user-defined pictures on the page, user-defined pictures often appear, especially off-site pictures that are deleted or inaccessible. Therefore, it is necessary to determine whether the picture can be loaded successfully, otherwise it will automatically change to "Unable to System pictures such as "Find Picture".

The method is very simple

Add a very simple JS code to the BODY

document.body.οnlοad=getImg;
function getImg()
{ 
 for(var i=0;i<document.images.length;i++)
 {
  if(document.images[i].readyState!=&#39;complete&#39;)
   document.images[i].src="/images/no.JPG";
 }
}
Copy after login

Or judge the readyState on the onload of each picture

Related learning recommendations: javascript video tutorial

The above is the detailed content of How to determine whether the image in the web page is loaded successfully or failed?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template