Detect specifically a “failed to load script” ErrorEvent

Viewed 69

How can an event handler for error discern whether this is a “the script failed to load” error?

When a script element specifies to load a script from a URL, but the script fails to load, the JavaScript engine will dispatch an event with type 'error' to that script element.

So an event handler can be written to catch this:

    window.addEventListener('error', ((event) => {
        console.error('ErrorEvent:', event)
        document.documentElement.classList.add('load-fail')
    }), true)

Many other kinds of errors might cause an event of type error to be dispatched to that element. In that case treating it as “loading the script failed” is incorrect.

How can I write an event handler that can distinguish specifically “the script failed to load” errors, in a way that detects this for all standards-compliant browsers?

0 Answers
Related