find image width and height

A

Atul

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?

function check(sel) {
if(sel != "") {
document.getElementById('newImg').src = sel;
document.getElementById('newImgDiv').style.visibility = "visible";
}
}

<input id="file" type="File" size="8" name="file"
onchange="check(this.value);">
<div id="newImgDiv" style="visibility:hidden">
<img id='newImg' onerror="alert('Please choose a valid image
file');">
<div>

- thanks
 
L

-Lost

Atul said:
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.

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);
}

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."
 
S

Simon Krajewski

-Lost said:
Atul said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top