How to capture the missing resources on the client side?

I am trying to capture instances of missing resources by adding the following code snippet to my <head> site:

 window.onerror = function(message, file, line) { console.log('Error:', message, file, line); } 

But it only seems to capture JS errors, not common DOM errors. I tried consciously: 1) creating <script> tags that point to nonexistent files, and 2) creating <img> tags that point to nonexistent images. Does not fire an error event.

My questions:

  • What errors occur while listening to onerror ?
  • How can I capture these errors? Why is this code not working?
+4
source share
1 answer

If there is a specific element that, in your opinion, may have a nonexistent image or script, and you do not need to catch things at all, you can add onerror to those specific img or script tags:

 <img src="http://something/that/might/not/exist.png" onerror="alert('Error!');"> 
+1
source

All Articles