function find_image_dm(imgsrc){
window.imgwidth = "";
var img = new Image();
img.onload = function test() {
var width = this.width;
var height = this.height;
if (height > 300) {
var x = height/300;
window.imgwidth = Math.ceil(width/x);
}
}
img.src = imgsrc;
return(window.imgwidth);
}
I want to return window.imgwidth so i can use it else where but if I try to alert it or anything like that it shows up blank. How do I return its value. window.imgwidth is my attempt to create a global variable. I do not need the width of the window
The problem with your attempt is that the
onloadcallback executes asynchronously. Because of this, you cannot reliablyreturnanything since theonloadcallback is executed at some future time. Instead of thinking in terms of return values, think about passing a callback function parameter to yourfind_image_dmfunction:Which you’d use like this:
Demo: http://jsfiddle.net/mattball/pTKW4/