Here is a simple script:
var _URL = window.URL || window.webkitURL;
var img;
var img_width = 0;
var img_height = 0;
var f = $(this)[0].files[0];
if (f != null) {
if (f.type == 'image/jpeg') {
img = new Image();
img.onload = function () {
img_width = this.width; //why not $(this)??
img_height = this.height;
alert(img_width + ' x ' + img_height); //1st alert
};
img.src = _URL.createObjectURL(f);
alert(img_width + ' x ' + img_height); //2nd alert
};
};
The img_width
& img_height
are a global var and its value is reset with img.onload
. When the script is executed, the first alert
displays the width and height. However the 2nd alert
always displays 0 x 0
. Why the gloabl var img_width
and img_height
were not reset within img.onload
for late use in main thread?