Determining Image Dimensions with JavaScript
Need a way to retrieve the height and width of an image on your web page using JavaScript or jQuery? Here's how to do it effortlessly:
You can programmatically get the image and check its dimensions using JavaScript. Here's how:
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 code initializes a new image object and defines the onload event handler to be executed once the image is loaded. Within the handler, we can access the width and height properties of the image object to retrieve its dimensions. Customizing the URL in the src attribute allows you to obtain the dimensions of any image you specify.
The above is the detailed content of How Can I Get an Image's Dimensions Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!