Home  >  Article  >  Web Front-end  >  Use Javascript to determine whether the image exists, and if it does not exist, display the code of the default image_javascript skills

Use Javascript to determine whether the image exists, and if it does not exist, display the code of the default image_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:16:231671browse

Use the onerror event of the image object to judge. If an error occurs, replace the src of the image object with the URL of the default image.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute ]


The following is the judgment through js
Use javascript to judge whether the specified image file exists:
For example, judge this picture Whether the address exists.
If it does not exist, re-detect the image every few seconds. If the address is valid, it will prompt that the address is valid
Copy code The code is as follows:



The above returns true if the image exists
Copy code The code is as follows:



If the image exists, return GIF89aX
Copy code Code As follows:



Because the picture does not exist, return that the picture does not exist!
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