Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'width' does not exist on type 'GlobalEventHandlers'

I would get the size of an image, following this tutorial, but I get a TypeScript error:

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';

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

Property 'width' does not exist on type 'GlobalEventHandlers'.

What to do?

like image 855
János Avatar asked Dec 03 '25 17:12

János


1 Answers

The load handler's this isn't typed to the image. Reference the image directly instead.

const img = new Image();
img.onload = function() {
  console.log(img.width + 'x' + img.height);
}

Or use addEventListener.

img.addEventListener('load', function() {
  console.log(this.width + 'x' + this.height);
})
like image 136
CertainPerformance Avatar answered Dec 05 '25 07:12

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!