Alex Rivera | Logout

How to verify background (css) image was loaded?

Asked 2009-12-18T11:17:07.417
35

I have the following CSS class that I'm applying on a <td> tag:

.bg {
   background-image: url('bg.jpg');
   display: none;
}

How can I tell with JavaScript/jQuery that the background image finished loading?

Edit
Report

1 Answer

61

The only way I know of to do this is to load the image using Javascript, and then set that image as the backgroud.

For example:

var bgImg = new Image();
bgImg.onload = function(){
   myDiv.style.backgroundImage = 'url(' + bgImg.src + ')';
};
bgImg.src = imageLocation;
answered 2009-12-18T11:22:38.297

Your Answer