0

I'm trying to get image dimensions. here is my attempt with .load

    function checkgallery_imagesize(src) {
          var dimensions = [];

          $("<img/>").load(function(){
                dimensions.push(this.width);
                dimensions.push(this.height); 
            }).attr("src", src);

          return dimensions;
         }

var src = 'example.png';
dimensions = checkgallery_imagesize(src);

console.log(dimensions[0]); //width;
console.log(dimensions[1]); //height;

When i use console.log(dimensions). I see the dimensions in the array but i can't access the array. dimensions[0] and dimensions[1] are both undefined

isherwood
  • 58,414
  • 16
  • 114
  • 157
user892134
  • 3,078
  • 16
  • 62
  • 128
  • 3
    Your console logs are running before the load function completes. Move them inside it. – isherwood Oct 08 '18 at 18:35
  • i need the console.log outside the function. I'm going to be using width and height outside the function. – user892134 Oct 08 '18 at 18:36
  • 1
    Then you'll need to arrange your program to wait for `checkgallery_imagesize()`. Promise or await or whatever. – isherwood Oct 08 '18 at 18:37
  • 1
    Also you should move the `var dimension` declaration outside the function. As the error says 'dimension is undefined' where you are accessing (console.log) – Selvakumar Arumugam Oct 08 '18 at 18:37

0 Answers0