Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify background (css) image was loaded?

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?

like image 894
thedp Avatar asked Dec 18 '09 11:12

thedp


People also ask

Why is my background image not showing up CSS?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

Where is background image inspect?

To download a background image, Ctrl -click or right-click the image and, from the menu that appears, select View Source or View Frame Source. Find the URL for the background image in the document's <body> tag (it should be near the top).

Why is my Div background image not showing?

Assuming that your . png file actually exists, try setting the background size and repeat tags. If that doesn't work, try checking in your browser's developer tools for the response codes and making sure that the url is correct.


1 Answers

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; 
like image 121
Jamie Dixon Avatar answered Oct 12 '22 00:10

Jamie Dixon