This would be a Chrome bug.
Per specs, the error
event should fire on the <object>
element (and it does in Firefox) both if the URL parsing failed, or if the resource loading failed.
There is though some kind of a grey area where if the request didn't end in a network error, and if the resource does not have associated Content-Type metadata, then it will just silently fail...
This seems counter-intuitive and might require a specs bug.
Anyway, I think Chrome fails to recognize network errors as it should, so the solution is probably to open a chrome issue so they fix it. I added a comment on BUG 853140, let's see how it goes
The workaround is to use an <iframe>
instead. <object>
will probably get deprecated soon anyway.
Note that setting a type
attribute to an unsupported type or just having it in the response's MIME type, Chrome will fire the event, but it still faces a new issue where it fires this event synchronously, even before we have time to attach any handler. So we have to force it to fire again by setting the data
attribute again:
document.querySelectorAll('object')
.forEach( el => {
el.onerror = e => console.log( "error", el );
el.data = el.data; // force fire again
} );
<object data="/foo"></object> <!-- doesn't fire -->
<object data="/foo" type="image/svg+xml"></object> <!-- doesn't fire -->
<object data="/foo" type="foo/bar"></object> <!-- does fire -->
<object data="data:foo,"></object> <!-- does fire -->