Use JS to get picture original size _javascript tips

Source: Internet
Author: User

The size of the picture displayed in the browser is not necessarily his true height and width, like the following, we give him a wide and high style

The size shown in the browser is 25px. So how do we get the real size of the picture? , the following code implements this functionality

Copy Code code as follows:

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<body>

<script>
Set time delay to ensure picture loading 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>

Note: The above code I have tested on ie7+ and chrome, because there is no IE6, so can not test.

Very good code, I most of the projects are in use, we rest assured that use it

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.