3

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
}
6
  • Have you tried anything yet? Can you show us your code? Commented Jan 16, 2014 at 20:49
  • look at this: stackoverflow.com/questions/13673210/… Commented Jan 16, 2014 at 21:16
  • @CodeToad That will not work. What that code does is it collects all the elements with image-based CSS attributes and makes a corresponding img tag that it listens to (as others have suggested). This assumes perfect caching, which may not be available, and may make another network request. What if the image callback succeeds, but a network request fails for the actual CSS object? Or vice versa. Commented Jan 16, 2014 at 23:03
  • If the worse comes, you can just add a <img> tag with display: none then use imageLoaded (desandro.github.io/imagesloaded). However this is just a workaround. Commented Jan 16, 2014 at 23:09
  • @tyteen4a03 Yeah, I've tried that and while that works sometimes, I can't guarantee that works everytime on platforms that have poor or no caching. What if I make the tag, the image loads, and I set the background-url and it doesn't load for that one? Programmatically it looks like everything is fine, except the background-image never loads. Commented Jan 16, 2014 at 23:43

1 Answer 1

1

Would it be possible for you to load the image with JS, and if it loaded then apply the class to element.

Something like this:

var img = new Image();
img.onload = function() {
    // add class
}
img.src = '/images/myImage.jpg';

The idea would be to have JS make the call for the image and then once the browser has it in the cache then apply the class and it should load.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.