Home > Web Front-end > JS Tutorial > js determines whether the background image is loaded successfully using img's width implementation_javascript skills

js determines whether the background image is loaded successfully using img's width implementation_javascript skills

WBOY
Release: 2016-05-16 17:32:56
Original
1275 people have browsed it

Non-background pictures can be judged using img's onerror, but as for background pictures, I just accidentally discovered a method that is feasible by visual inspection, but I am not sure whether it is always feasible.

Copy code The code is as follows:

var img=new Image();
img .src="image address";
if(img.width==0){//Indicates that the image loading failed}
else{//Successful}

This method It is generally feasible, but we have tested whether it is feasible when the image is very large or the loading speed is very slow.
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template