3Dom
3Dom

Reputation: 805

jQuery trigger on image load (hidden image)

OK, so I've heard that JS cannot trigger events on CSS background image load. So, what I've seen some people suggest, is to trigger the event on an img tag load. But you obviously would need to hide this image so it doesn't get displayed?

Do you need to put this image somewhere on your page and then do something like: display:none css?

Otherwise it would obviously be a nice big image you don't want to see...

I would have thought most half decent browsers wouldn't load an image if it was display:none; . Or could JS just load the image into the cache, with HTML being completely unaware of it?

Am I wrong?... If so, it is one of those rare moments in life where being wrong is awesome.

Can't wait for HTML6. Also can't wait for nobody to support it as usual, and then be another cynical idiot that bangs on about it for another 5 years.

Upvotes: 0

Views: 1099

Answers (1)

René
René

Reputation: 6176

Display none could actually not load the image at all, which nullifies exactly what you want to accomplish.

You can easily add an image to the 'cache'.

var myImage = new Image();
    myImage.onload = function(){
        // What you want to do!
    };
    myImage.src = 'myimage.png';

Upvotes: 2

Related Questions