How to get image size in Javascript - get width and height

How can I measure the dimensions of specific image using Javascript ? For example how can I get image width and height using onClick ?
0
give a positive ratinggive a negative rating
13 Jun 2022 at 06:51 PM
Hi,

To get image width and height using Javascript, you can use the following function:


<script>

function imageSize(url) {

var img = new Image();
img.onload = function(){
var imageWidth = img.width;
var imageHeight = img.height;
alert('Width: '+imageWidth+' / Height: '+imageHeight);
}
img.src = url;
}

</script>

<a href="javascript:void(0);" onClick="javascript: imageSize('https://www.example.com/image.png');">Get image size</a>

1
give a positive ratinggive a negative rating
14 Jun 2022 at 11:09 AM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us