I was trying to write a function that would check if the image I chose via input[type=file] is exactly 700 px wide and if not - reject this image. I succeeded, however, sometimes this function randomly returns 0 and I need to load the same image a couple of times until it reads its width correctly. Any ideas on how to improve this code? Thanks in advance!
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
var img = new Image;
img.src = e.target.result;
if(img.width == 700) {
DoSomeStuff();
} else {
alert("This image has to be 700 px wide, but is " + img.width + " px wide. Try again!");
input.value = "";
}
};
reader.readAsDataURL(input.files[0]);
}