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

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us