$( img ).height()
returns 0
in chrome, but it returns the actual height in IE and firefox.
Whats the actual method to get the height of the image in chrome?
$( img ).height()
returns 0
in chrome, but it returns the actual height in IE and firefox.
Whats the actual method to get the height of the image in chrome?
As Josh mentioned, if the image has not fully loaded yet, jQuery won t know what the dimensions are yet. Try something like this to ensure you re only attempting to access it s dimensions after the image has been completely loaded:
var img = new Image();
$(img).load( function() {
//-- you can determine the height before adding to the DOM
alert("height: " + img.height);
//-- or you can add it first...
$( body ).append(img);
//-- and then check
alert($( body img ).height());
}).error( function() {
//-- this will fire if the URL is invalid, or some other loading error occurs
alert("DANGER!... DANGER!...");
});
$(img).attr( src , "http://sstatic.net/so/img/logo.png");
My assumption is that this function is being called before the image is finished loading. Can you try putting a delay on the function to see if this is actually the case? If this is the case, you can cheat by using a timer before running the function.
It is a bit more complicated to detect when an image has been loaded. Have a look at this script for some ideas -- maybe it would work for you.
Preloading the image and setting up a callback function might be what you re looking for:
// simple image preloader
function getHeight(el,fn) {
var img = new Image();
img.onload = function() { fn(img.height); };
img.src = el.attr("src");
}
$("img").each(function(){
getHeight($(this),function(h){
// what you need to do with the height value here
alert(h);
});
});
A slightly ugly but effective fix that I used was use a backend language, in my case php, to get the height and set it on the image before sending it to the browser.
it s ugly, but simple and quick
Try using an image pre-loader, here s one I wrote for an answer to another question.
Having just done some experiments with this I ve found that
$(this).height();
Returns 0 - however if you just use plain old JS
this.height;
I get the correct image height.
Just had the same issue: set the width and height beforehand via CSS or HTML.
I have a QCView that loads a Quartz file which gives you iSights feedback (basically like a QTCaptureView) Everything displays fine The button simply takes a snapshot using the following simple ...
Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...
In .NET how can I transform an image into a trapezoid. The Matrix class supports rotation, shear, etc, but I can t see a trapezoidal transformation. I m using the usual System.Drawing.* API, but I m ...
What are my options for rotating an image on a web page through the Y-axis? I m not sure if I even have the terminology right. Is this called a rotate or a transform. Most of the searches that I ve ...
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
I m developing a new site that graphs some operational metrics. As such about a dozen charts/graphs will be displayed on the site. I want to be able to have them dynamically scale down (within ...
Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...
I need to take a full color JPG Image and remap it s colors to a Indexed palette. The palette will consist of specific colors populated from a database. I need to map each color of the image to it s "...