-Lost schrieb:
Atul wrote:
>I am unable to find image height and width in mozilla firefox. My code
is working in IE but not in Mozilla. How can i find image width and
height in mozilla?
Two options.
<img src="file.x" />
Assuming it is the 0th image, and the image is loaded:
document.images[0].width and .height
The collection can also be referenced by name if a name attribute is
supplied to your image.
This is just a different way of referencing to the image object within
the DOM. Whether you use document.images, pass a reference to your
function or refer to it using document.getElementById, there's still no
information about image width and height during script execution, which
is why an alert(imageInDOM.width) usually returns the width of the image
that was loaded prior to the current image (unless the current image was
loaded and cached before, it might work in this case). After setting a
..src attribute, script execution will not halt until the corresponding
image finished loading, so if you do
imageInDOM.src = "myImage.gif";
alert(imageInDOM.width);
you are likely to receive wrong width information. Tested in both IE6
and FF2.
Or (thanks to scripts.contact for reminding me not to be so thickheaded):
var newImg1 = new Image();
newImg1.src = 'file.x';
newImg1.onload = function()
{
alert(newImg1.width + 'x' + newImg1.height);
}
This looks better because the onload method should only be invoked once
the image finished loading (yea really!), in which case width and height
information are available.
Bearing in mind however, unless I have missed something, the second
method will not fire as is for Opera.
See "Extract Width and Height of image" by "SM."
Regards
Simn