0

I would like to load image and return from onload boolean value. I've tried to solve it but I failed.

    var reader = new FileReader();
    var image  = new Image();
    reader.readAsDataURL(input.files[0]);
    reader.onload = function (e) {
        image.src    = e.target.result; 
        var valid = false;
        valid = image.onload = function() {
            if (thi.width > 200) {
                valid = true;
            }
        return valid;
    }();

    }
    - here i would like to return "valid" 

I dont know how to achieve it :(

i want do do something like this:

function validateImage(input) {
if (input.files && input.files[0]) {
    var reader = new FileReader();
    var image  = new Image();
    reader.readAsDataURL(input.files[0]);
    reader.onload = function (e) {
        image.src    = e.target.result; 
        var valid = false;
        valid = image.onload = function() {
            if (thi.width > 200) {
                valid = true;
            }
            return valid;
        }();
        return valid;
    }
}
here i would like to return "valid"
return valid
}
Michał Wolnicki
  • 265
  • 4
  • 12
  • 1
    Where do you want to return it to? Who is calling the function, and what are they doing with the return value? – Barmar Sep 12 '15 at 21:51
  • The `onload` function is called asynchronously by the browser, it doesn't do anything with the return value. – Barmar Sep 12 '15 at 21:54
  • i added whole function. Still I don't know how should I do this – Michał Wolnicki Sep 13 '15 at 07:41
  • You can't return it. If you want to display a message to the user, you have to do it in the callback function. – Barmar Sep 13 '15 at 23:48
  • If you understand the general idea of asynchronous functions, as explained in the linked question, it should become clear why it doesn't make sense to return from the `onload` function. – Barmar Sep 13 '15 at 23:49

0 Answers0