I wonder if that is possible to handle missing resources of a web page all togather including missin images from background: url(...) from CSS.
I have checked document.addEventListener("error", eventHandler, true);, window.onerror and also performance.getEntries() but none of them reports missing images referenced from CSS even from inlined styles.
Best base to also handle missing resources from other origins e.g. broken fonts referenced from CDNs.
A possible solution could be to list all resources using window.performance.getEntriesByType("resource"), then try to download each of them using ajax to see if it produces a 404. However there are 2 major drawbacks to consider:
Anyway here is a starting point:
window.onload = function() {
setTimeout(function() {
var resources = window.performance.getEntriesByType("resource");
resources.forEach(function (resource) {
$.get(resource.name).fail(function(e) {
var msg = 'Failed to load resource '+resource.name+' requested by '+resource.initiatorType;
var div = $('<div/>').text(msg);
$('body').append(div);
});
});
}, 1000);
};
body {
background-image: url('bar.jpg');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<img src="foo.jpg"/>