Handling Broken Images with jQuery or JavaScript
Problem:
A web page with numerous images encounters broken image instances due to unavailability.
jQuery Approach:
JavaScript onError Event:
An alternative approach involves handling the onError event for each image to reassign its source directly.
function imgError(image) { image.onerror = ""; image.src = "/images/noimage.gif"; return true; }
<img src="image.png" onerror="imgError(this);"/>
<img src="image.png" onError="this.onerror=null;this.src='/images/noimage.gif';" />
Browser Compatibility:
For the onError event handling approach, please refer to the compatibility table at:
http://www.quirksmode.org/dom/events/error.html
The above is the detailed content of How Can I Efficiently Handle Broken Images in My Web Pages Using jQuery or JavaScript?. For more information, please follow other related articles on the PHP Chinese website!