I'm working on a variety of really low-powered devices and would like to switch to background-image for a variety of reasons instead of using an actual img tag. One issue that seems to prevent me from doing this is there is no callback associated with an background-image loading or not, which is critical (since failure happens on these devices).
I've seen a lot of recommendations to use an image tag's callback to check the resource then delete the resource, but may not be robust enough for me. For example, if the platform doesn't support caching, that could create two network requests/downloads for the same image. Furthermore, there's always the chance that the image loads successfully for the image, but fails for the background-image.
Is there a separate callback I could attach to something like a div using addEventListener to monitor the its network-based CSS requests? Or if not a callback, at least verify the background image has in fact loaded?
<style>
.class {
background-image: url('./static/example.jpg');
}
</style>
<div class="foo"></div>
function onSuccess() { // this called if ./static/example.jpg loaded
}
function onFailure() { // this called if ./static/example.jpg failed to load
}
<img>tag withdisplay: nonethen useimageLoaded(desandro.github.io/imagesloaded). However this is just a workaround.