Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  164] [ 2]  / answers: 1 / hits: 54983  / 13 Years ago, thu, september 15, 2011, 12:00:00

I have a JS function where a value is computed and this value should be returned but I get everytime undefined but if I console.log() the result within this function it works. Could you help?



function detect(URL) {
var image = new Image();
image.src = URL;
image.onload = function() {
var result = [{ x: 45, y: 56 }]; // An example result
return result; // Doesn't work
}
}

alert(detect('image.png'));

More From » image

 Answers
56

I get it myself:



I didn't know that I can assign a variable to that (for me looking already assigned) onload.



function detect(URL) {
var image = new Image();
image.src = URL;
var x = image.onload = function() {
var result = [{ x: 45, y: 56 }]; // An example result
return result;
}();
return x;
}

alert(detect('x'));

[#90083] Wednesday, September 14, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samsons

Total Points: 331
Total Questions: 97
Total Answers: 106

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;