<p>
A little proof of concept to test if we can log failing image load. see console and javascript panel.
</p>
<img src="idontexist.png">
<img src="https://placekitten.com/200/300">
JavaScript
if (console && console.clear) {
console.clear();
}
init();
function init() {
// // This does not work: (load/error/abort events do not seem to bubble up)
// $(document).on('load error abort', 'img', function(e){
// console.log(1, this.src, e.type);
// });
// This works, but impractical for all new images appearing later:
// $('img').bind('load error abort', function(e){
// console.log(2, this.src, e.type);
// });
// // This works, also for img elements created later:
// // use capturing events: addEventListener(..., ..., true)
// // https://stackoverflow.com/questions/14983988/is-bubbling-available-for-image-load-events
// // https://stackoverflow.com/a/24611104/1158769
// document.addEventListener(
// 'error',
// function(e){
// if( e.target.tagName == 'IMG'){
// console.log(3, e.target.src, e.type);
// }
// },
// true // <-- useCapture
// );
// jQuery.on() with added captureEvent = true: this works:
$on(document, 'load error abort', 'img', function(e){
console.log(4, this.src, e.type);
}, true); // note the captureEvent = true here
// elements created after event listener is set should also work:
$(document.body).append(`<img src="idontexist2.png">`);
$(document.body).append(`<img src="idontexist2.png">`);
$(document.body).append(`<img src="https://placekitten.com/200/200">`);
$(document.body).append(`<img src="https://placekitten.com/200/200">`);
}
// native js variant on jQuery.on() , with additional useCapture parameter
// also see http://youmightnotneedjquery.com/#delegate
// $(document).on(eventName, elementSelector, handler);
function $on(eventTarget, eventName, elementSelector, handler, usecapture){
if (typeof eventTarget === 'undefined' || ! eventTarget) {
eventTarget = document; // default fallback
}
var eventNames = (''+eventName).trim().split(/\s+/); // support multiple events seperated by whitespace
eventNames.forEach(function(eventName){
eventTarget.addEventListener(eventName, function(e) {
for (var target...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.