JavaScript: a simple method for obtaining the original size of page images, javascript pages

Source: Internet
Author: User

JavaScript: a simple method for obtaining the original size of page images, javascript pages

This example describes how to obtain the original size of a page image by using JavaScript. We will share this with you for your reference. The details are as follows:

The original width and height are obtained through the Image () object.

This method is not so troublesome. You can directly create an Image () object and assign the src value of img to it to obtain it.

Var img = new Image (); img. src = $ ("# target "). attr ("src"); if (img. complete) {alert ('width: '+ img. width + ', height' + img. height); img = null;} else {img. onload = function () {alert ('width: '+ img. width + ', height' + img. height); img = null ;};}

And do not worry that the new Image object will have an additional http request. The browser has cached the image after loading it. you have no problem with the new N Image objects. Of course, the memory will consume, therefore, img is set to null after use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.