admin管理员组

文章数量:1317915

At first image height is ing as zero, after that by refreshing the page i am getting its actual height. I need to get its height at first time? could any body helps me?

$j(document).ready(function(){
           var imgV = new Image();
           imgV.src=".JPG";
           $j("#imgD").html(imgV.height);
           alert(imgV.height);
});


<div id="imgD"></div>
<img src=".JPG" />

At first image height is ing as zero, after that by refreshing the page i am getting its actual height. I need to get its height at first time? could any body helps me?

$j(document).ready(function(){
           var imgV = new Image();
           imgV.src="http://www.kerrvilletexascvb./Riverside%20Nature%20Center3.JPG";
           $j("#imgD").html(imgV.height);
           alert(imgV.height);
});


<div id="imgD"></div>
<img src="http://www.kerrvilletexascvb./Riverside%20Nature%20Center3.JPG" />
Share Improve this question asked Apr 2, 2009 at 5:30 Shiva Srikanth ThummidiShiva Srikanth Thummidi 2,9084 gold badges27 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

You don't have to insert an image into the DOM to get its dimensions:

$("<img />")
    .attr("src", "http://www.google./intl/en_ALL/images/logo.gif")
    .load(function() {
        alert(this.width);
        alert(this.height);
    })
;

The image size is only available when the image is loaded. Likely when you reload the page, the image is instantly served from cache, so its size is instantly available.

If for whatever reason you need to get the size before you display an image, you can display it off-screen to begin with. Add an onload handler to it that will get called when the image is ready - you can then inspect it's size, and attach it where needed.

By displaying off-screen, I mean stick it into a 1x1 pixel div with overflow: hidden style, somewhere on the bottom of the page.

本文标签: javascripthow can l get the image sizebefore it loads into htmlStack Overflow