The page begins to load.
<img id="logo" src="/logo.png">
Then we run a bunch of JavaScript and by the time we get to the error handling for the <img>
above, it has experienced an error... /logo.png
returns 404 or 500 or something.
Now, this JavaScript:
document.getElementById('logo').onerror = () => {
console.log('logo img did not load');
}
is useless because the onload and onerror callbacks would have already been called.
A solution to see if it loaded successfully would be to check:
document.getElementById('logo').complete
but this shows true even if there was an error, and looking in the JavaScript console and Googling has shown me nothing similar to this for error checking.
Does anyone know of any way to see if there was an error when the <img>
tried to load.
I don't want to use the following:
let img = new Image();
img.onerror = () => {
console.log('logo img did not load');
}
img.src = '/logo.png';
Edit: I am unable to add any JavaScript attributes to the html. That would be handy, and a solution in some situations, but it is not a solution for my situation. So this:
<img id="logo" src="/logo.png" onerror="errorHandler()">
won't work for me.