I am creating a jQuery plugin.
How do I get real image width and height with Javascript in Safari?
Following works with Firefox 3, IE7 and Opera 9:
var pic = $("img")
// need to remove these in of case img-element has set width and height
pic.removeAttr("width");
pic.removeAttr("height");
var pic_real_width = pic.width();
var pic_real_height = pic.height();
But in Webkit browsers like Safari and Google Chrome values are 0.
this works for me (safari 3.2), by firing from within the
window.onload
event:I've done some workaround utility function, using imagesLoaded jquery plugin: https://github.com/desandro/imagesloaded
You can use this by passing url, function and its context. Function is performed after image is loaded and return created image, its width and height.
Here's a cross browser solution that triggers an event when your selected images are loaded: http://desandro.github.io/imagesloaded/ you can look up the height and width within the imagesLoaded() function.
I checked out the answer of Dio and it works great for me.
$('#image').fadeIn(10,function () {var tmpW = $(this).width(); var tmpH = $(this).height(); });
Make sure that you call all your functions aso. that handle with the image size in the recaller function of fadeIn().
Thanks for this.
If the image is already used, you sholud:
set image simensions to initial
image.css('width', 'initial'); image.css('height', 'initial');
get dimensions
var originalWidth = $(this).width(); var originalHeight = $(this).height();
Jquery has two properties called naturalWidth and naturalHeight, you can use in this way.
Where my-img is a class name used to select my image.