Is it possible to catch CSS background | background-image: url(..);
errors (e.g 404) with JavaScript and make corrections?
Only option I can think of right now is to loop over all elements in page, check background
and background-image
property, if present, try to make corrections. This is undesirable fix as you can probably guess.
I've notices that if I use custom Image src
setter, it seems that CSS url
does make an Image in background and operate on that but even if I change its src
, url('images/example.jpg')
still stays the same and points to wrong path.
const {get, set} = Object.getOwnPropertyDescriptor(Image.prototype, 'src');
Object.defineProperty(Image.prototype, 'src', {
set(value) {
// this gets called even for CSS url()
return set.call(this, value);
},
get() {
return get.call(this);
}
});