tags:

views:

51

answers:

2

now i get the image's width and height when onload function of img . my problem is, the image original width = 500px but document.getElementId(id).offsetWidth gives only 300px and also for height. Please help me how can i get original width and height of image

A: 

At least Gecko- (Firefox) and WebKit-based (Safari, Chrome) browsers should implement properties naturalWidth and naturalHeight for image elements:

var image = document.getElementById("imageid");
if (image.naturalHeight === undefined) {
  alert("Your browser doesn't support this example!");
} else {
  var message = "The original size of the image:";
  message += "\n height: " + image.naturalHeight + "px";
  message += "\n width: " + image.naturalWidth + "px";
  alert(message);

}

kkyy
@kkyy why not just image.height?
c0mrade
@c0mrade Because if the image has been scaled the image.width and image.height properties return the scaled size, not the original one. At least in firefox.
kkyy
@kkyy alright tnx for the info
c0mrade
A: 

sorry my browser doesn't support with naturalHeight and naturalWidth..

Jeny