javascript - Exit function only when return is called -
i have input file used upload images. however, have validate size before upload. below code i've tried on.
i have parent function, calls method,validateimagesize( ):
$('input:file').change(function (e) { if (validateimagesize(this)) // else alert("wrong size"); });
and method shown below:
var validateimagesize = function (input) { var reader = new filereader(); reader.onload = function (e) { var img = new image(); img.onload = function (e) { return this.height == 40 && this.width == 40 ? true : false; } img.src = e.target.result; } reader.readasdataurl(input.files[0]); };
the method validateimagesize()
returns 'undefined
' parents, cause have yet executes onload functions.
the output need either true/ false. perhaps structure of codes incorrect.
how can solve this?
use callback
, below:
var validation_callback = function(isvalid) { if (isvalid) { // } else { alert("wrong size"); } }; $('input:file').change(function(e) { validateimagesize(this, validation_callback); }); var validateimagesize = function (input, callback) { var reader = new filereader(); reader.onload = function (e) { var img = new image(); img.onload = function (e) { var isvalid = this.height == 40 && this.width == 40 ? true : false; callback(isvalid); }; img.src = e.target.result; }; reader.readasdataurl(input.files[0]); };
Comments
Post a Comment