When a div is hidden (display:none
) the browser will not load the images that are inside it. Is there a way to tell the browser to load the image?
I've added some preloading, wrapped it into a function and that does the trick:
function getImageDimension(el, onReady) {
var src = typeof el.attr === 'function' ? el.attr('src') : el.src !== undefined ? el.src : el;
var image = new Image();
image.onload = function(){
if(typeof(onReady) == 'function') {
onReady({
width: image.width,
height: image.height
});
}
};
image.src = src;
}
Can be used as:
getImageDimension($('#img1'), function(d){ alert(d.height); });
var url = 'http://urology.jhu.edu/patrickwalsh/photo1.jpg';
getImageDimension(url, function(d){ alert(d.height); });