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