PDA

View Full Version : Width, Height is read only for Image Object???



pman
05-23-2007, 03:04 AM
Hi there,

I just googling about reference of Javascript Image object and found the following site as the first result.

http://www.comptechdoc.org/independent/web/cgi/javamanual/javaimage.html

It says that height and width is read only. Isn't that a mistake? I think I have seen couple of other sites that mentioned those two properties as read only value.

I was able to do the following and change the height and width value, which worked perfectly fine.



var myImg = new Image() ;
myImg.src = "http://www.cortland.edu/flteach/mm-course/forms1.jpg" ;
alert(myImg.height) ; alert(myImg.width) ;
myImg.height = 500 ;
myImg.width = 600 ;

document.getElementById('imageTest').appendChild(myImg) ;


______________________
Regards

Pman
http://www.pmansLab.com

Trinithis
05-23-2007, 04:33 AM
I don't know, but if it is, then it's the browser doing the handy work for you. I personally like the use of styles:

Example:
img.style.width = "5px";

don't forget to put in your units (px, in, cm, em, etc.)!

Bob90
05-23-2007, 08:07 AM
It might reflect the fact that img.width and img.height are depreceated.

Use

img.style.width instead.

jscheuer1
05-23-2007, 08:19 AM
In javascript, an image's dimensions are read only. An image tag's dimensions are another matter. The confusion arises due to the unconventional way you have chosen to create an image tag:


var myImg = new Image() ;
myImg.src = "http://www.cortland.edu/flteach/mm-course/forms1.jpg" ;
myImg.height = 500 ;
myImg.width = 600 ;

document.getElementById('imageTest').appendChild(myImg) ;

This would more accurately be expressed:


var myImg = document.createElement('img') ;
myImg.src = "http://www.cortland.edu/flteach/mm-course/forms1.jpg" ;
myImg.height = 500 ;
myImg.width = 600 ;

document.getElementById('imageTest').appendChild(myImg) ;

The browser will not know the dimensions of the actual image file until they have been loaded. Regardless of this information, the browser will accept your dimensions (in most cases), scaling the presentation of the image to them. However, the size of the image file itself remains unchanged - hence read only.