Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Global JavaScript event for missing resources

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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:

  • this isn't an event listener, you have to execute the check after a reasonable amout of time to let the browser load all resources, or execute it periodically;
  • you must avoid to check for dynamic resources which could return a different result from the previous call, or the more dangerous case where a call to a resource produces an update of the data on the remote server (a very trivial example could be a view counter on an image);
  • there is a chance that the ajax call results in different status code even if the resource is static, for example if the client is on an unstable network and the connection goes down between the two calls.

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"/>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!