Home > Web Front-end > JS Tutorial > How Can I Get an Image's Dimensions Using JavaScript?

How Can I Get an Image's Dimensions Using JavaScript?

Linda Hamilton
Release: 2024-12-16 08:06:12
Original
538 people have browsed it

How Can I Get an Image's Dimensions Using JavaScript?

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';
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template