I have this code in jquery:
var ratio;
$("<img/>").attr("src", $(img).attr("src")).load(function() {
ratio = this.width / this.height;
alert(ratio);
});
This retuns the correct ratio, but when I try this
var ratio;
$("<img/>").attr("src", $(img).attr("src")).load(function() {
ratio = this.width / this.height;
});
alert(ratio);
It returns undefined.
I do not understand why this happens even though I have declared globally the ratio variable.
I use this in the following exemple:
working http://jsfiddle.net/7sUAY/5/
not working http://jsfiddle.net/7sUAY/6/
Thank you, Mugur
loadcallback is executed only when the image was loaded, whereas every code following$("<img/>").attr().load()is executed immediately. Otherwise, what would be the reason to provide a callback at all?