473,480 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

find image width and height

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
2 13400
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
-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3062
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
2
4127
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
5
2124
by: Csaba Gabor | last post by:
Is there any way to determine the pixel height and width of an original image? Specifically, If I have <IMG id=myImg src="pic.jpg" height=200 width=300> can I figure out what the original size...
15
31759
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and...
3
2473
by: Andy Baxter | last post by:
Is there a way in javascript to find out the size of an image you've just preloaded? I have the following object: function tourImage(imgName) { // used for passing image details to a...
4
2731
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
2
5055
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
68096
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
2
2056
by: everly | last post by:
Hi, I'm helping my friends band and making their MySpace. I messed something up and I can't figure out what it is. Everything is all out of whack on the bottom half of the page. The comments...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5357
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.