By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

find image width and height

P: n/a
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.visibil ity = "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

May 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

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

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 2 '07 #2

P: n/a
-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
May 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.