How to get image size (height & width) using JavaScript?

Saneef picture Saneef · Mar 8, 2009 · Viewed 1.2M times · Source

Are there any JavaScript or jQuery APIs or methods to get the dimensions of an image on the page?

Answer

Josh Stodola picture Josh Stodola · Mar 9, 2009

You can programmatically get the image and check the dimensions using Javascript...

const img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

This can be useful if the image is not a part of the markup.