0

I want to return the dimensions of a picture using Javascript and i want to ask you what is wrong with this code, because it does nothing.

Here' the code:

function getImgSize(url){
var img = new Image();
img.onload=function(){
    return {
        width:  img.width,
        height: img.height,
    };
};

img.src = url;

}

Thanks in advance!

Regen
  • 1
  • Where do you think the return inside the function goes to? You need to trigger some other function that uses the width/height in some way, or just console.log it inside the onload function. As written, you just return stuff to void 0 (aka nowhere ) – Shilly Jun 09 '17 at 13:07
  • You can not return from an asynchronous process – epascarello Jun 09 '17 at 13:07
  • Well, what do you want it to do? If a function returns something the caller has to handle that return value, or just ignore it. The return value of an event handler is indeed ignored. – Lennholm Jun 09 '17 at 13:08

0 Answers0