Not too sure why but I figured someone out here can educate me a bit on this. I am looking to set a global variable based off a jQuery loaded object. In my case I am loading an original image size from the server so I can proportionally resize it programatically. But I am stumped as to why I can alert or console log the height and width but cannot set a variable to those retrieved values outside the callback function?
HTML:
<img src='someimage.jpg' />
JS/jQuery:
var imgHeight, imgWidth;
$("img").load(function() {
imgHeight = $(this).height();
imgWidth = $(this).width();
console.log(imgWidth + " x " + imgHeight); // this works
});
console.log(imgWidth + " x " + imgHeight); // this does not
Is this a timing issue and what would be a better solution to load the image and set a variable based on that response?