1

I have some dynamic images inserted into my body with php and I need to get their height in order to position them properly. the issue is that jQuery will get me the height or width of each image except in explorer, for example:

HTML:

<img src="someimage.jpg" id="img_id" />

Jquery

alert($('#img_id').height())

In firefox it will automatically read from the image file the height and return the value, lets say 400px;

But in explorer it will return empty! It will only return a value if there is a height written on HTML or CSS:

<img src="someimage.jpg" id="img_id" style="height:400px" />

First, why explorer won't fetch the dimensions of the image, and second how can I fix this problem without generating the image styling for each image? Thanks

multimediaxp
  • 9,348
  • 13
  • 49
  • 80
  • Did you forget the # before the id? alert($('#img_id').height()) ? – ysrb Apr 26 '13 at 02:13
  • What version of jQuery are you using? – j08691 Apr 26 '13 at 02:15
  • You have to wait for the image to completely load before you can get its dimensions. Are you? – Blazemonger Apr 26 '13 at 02:28
  • ysrb id is there, little typo, I fixed it, j008691 I am using v1.9.1, @Blazemonger I use $(document).ready(), but the funny part is that firefox gets the heights, and natively .height() gets back the height even if it is not loaded, is not like I am inserting the image as a new element with JS, it is already in the HTML body as it is sent from the server, if I had appended a new child I would agree with you, Thanks guys. – multimediaxp Apr 26 '13 at 03:38
  • Images are NOT loaded on document.ready. You need to wait for window.onload or use a special load event. – Blazemonger Apr 26 '13 at 10:41
  • http://stackoverflow.com/questions/3877027/jquery-callback-on-image-load-even-when-the-image-is-cached – Blazemonger Apr 26 '13 at 10:46
  • With `$(document).ready(function(){...` the images can't be accessed if the visitor doesn’t have the images already loaded. In this case you need to use `$(window).load(function(){...`. – Cholesterol May 28 '13 at 17:34

0 Answers0