Trigger image onload using javascript or jquery -
my situation need check size of image using javascript , execute piece of code depending on width.here code
var width, height; var img = new image(); img.src = "pool.jpg"; img.onload = function () { width = this.width; height = this.height; console.log(width); } console.log(width); if (width > 0) { console.log('in condition check'); //some code executed } //some other code executed cannot put in onload portion
the issue img.onload
portion works after below code has finished execution.is there way trigger img.onload
function , go in synchronous way of code execution.
no, can't make code wait until external task (usually involving server) has finished execution.
you have put code in callback (or in function called callback) :
img.onload = function () { width = this.width; height = this.height; console.log(width); if (width > 0) { console.log('in condition check'); //some code executed } }
to build javascript applications, must learn deal event-based programming, of code act upon events, them user actions or asynchronous task completions.
(technically there way don't use it)
Comments
Post a Comment