Alas although most browsers support the picture element, IE11 is the notable exception [2] and a polyfill is required.
[0] https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pi...
[1] https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimed...
Wouldn’t using the <picture> element[0] allow this this? It’s pretty widely supported (every browser that supports WebP and Safari)[1] and allows a fallback to a JPEG in an <img> tag.
[0] https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pi...
For example, Firefox on macOS sent this Accept header for this request: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Chrome sent text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9
Edit: Not to mention that the onerror event handler on the <img> tag has always been able to find out if an image didn’t display [2].
[0] https://html.spec.whatwg.org/multipage/media.html#dom-naviga...
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_ne...
[2] https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageEl...
Basically if a browser does support webp, the script does nothing, but otherwise I think this script ends up supporting the image via javascript.
The only hope seems to be mobile linux initiatives like PinePhone, etc. But I'm not holding my breath.