Use js to get the original image size and js to get the size

Source: Internet
Author: User

Use js to get the original image size and js to get the size

The size of the image displayed in the browser may not be his real height or width. For example, we add a width or height style to the image like below.

In this way, the size displayed in the browser is 25px. So how can we get the real size of the image ?, The following code implements this function

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
<Body>

<Script>
// Set the delay to ensure image loading is complete
SetTimeout (function (){
Var
Real_width,
Real_height,
_ Im = document. getElementById ('image '),
Im = document. createElement ('img ');
Im. src = _ im. src,
Real_width = im. width,
Real_height = im. height;
Alert (real_width + '\ n' + real_height );
},500 );
</Script>
</Body>
</Html>

Note: The above code has been tested on IE7 + and chrome, and cannot be tested because IE6 is not installed.

Very easy-to-use code, which is used in most projects.

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.