I have an HTML page where several JavaScript, CSS and images files are referenced. These references are dynamically injected and user can manually copy the HTML page and the support files to another machine.
If some JS or CSS are missing, the browser complains in the console. For example:
Error GET file:///E:/SSC_Temp/html_005/temp/Support/jquery.js
I need somehow these errors reported back to me on the inline JavaScript of the HTML page so I can ask user to first verify that support files are copied correctly.
There's the window.onerror
event which just inform me that there's a JS error on the page such as an Unexpected Syntax error, but this doesn't fire in the event of a 404 Not Found error. I want to check for this condition in case of any resource type, including CSS, JS, and images.
I do not like to use jQuery AJAX to verify that file physically exists - the I/O overhead is expensive for every page load.
The error report has to contain the name of the file missing so I can check if the file is core or optional.
Any Ideas?
@alexander-omara gave the solution.
You can even add it in many files but the window handler can/should be added once.
I use the singleton pattern to achieve this:
Now, from any context call it as many times you want as the handler is attached only once:
you can use the onload and onerror attributes to detect the error
for example upon loading the following html it gives alert error1 and error2 you can call your own function e.g
onerror(logError(this);)
and record them in an Array and once the page is fully loaded post is with single Ajax call.I've put together the code below in pure JavaScript, tested, and it works. All the source code (html, css, and Javascript) + images and example font is here: on github.
The first code block is an object with methods for specific file extensions:
html
andcss
. The second is explained below, but here is a short description.It does the following:
check_file
takes 2 arguments: a string path and a callback function.ext
) of the given pathsrcFrom
[ext
] object method that returns an array of relative paths that was referenced in the string context bysrc
,href
, etc.For convenience, it resolves to relative path names and does not care about which protocol is used (http or https, either is fine). It also cleans up the DOM after parsing the CSS.
And here is the function that gets the file contents and calls the above object method according to the file extension:
To use it, simply call it like this:
Please feel free to comment and edit as you wish, i did this is a hurry, so it may not be so pretty :)
To capture all
error
events on the page, you can useaddEventListener
with theuseCapture
argument set totrue
. The reasonwindow.onerror
will not do this is because it uses the bubble event phase, and theerror
events you want to capture do not bubble.If you add the following script to your HTML before you load any external content, you should be able to capture all the
error
events, even when loading offline.You can access the element that caused the error through
e.target
. For example, if you want to know what file did not load on animg
tag, you can usee.target.src
to get the URL that failed to load.NOTE: This technically will not detect the error code, but if the image failed to load, is it technically behaves the same regardless of the status code. Depending on your setup this would probably be enough, but for example if a 404 is returned with a valid image it will not trigger an error event.
You can use XMLHttpRequest for files.
and use the Image class for images.