How Can I Get an Image's Dimensions Using JavaScript?
Dec 16, 2024 am 08:06 AMDetermining 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!

Hot tools Tags

Hot Article

Hot tools Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial
