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
coldplay.xixiOriginal
2020-06-30 15:08:204356browse

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";
 }
}

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!

Statement:
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

Related articles

See more