Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image naturalWidth return zero

Tags:

javascript

image naturalWidth return zero... that's it, why ?

var newimage = new Image(); newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg';  var width = newimage.naturalWidth; alert (width); 

HELP, i dont know why !

*** that path is good, the image show up !

like image 828
menardmam Avatar asked Oct 29 '09 17:10

menardmam


People also ask

What is img naturalWidth?

The naturalWidth property returns the original width of an image. For example, if you have an image that is originally 100 pixels wide. Then, you style the image with CSS/or by using the HTML "width" attribute to make it 500 pixels wide.

What is naturalHeight?

Definition and Usage The naturalHeight property returns the original height of an image. For example, if you have an image that is originally 200 pixels high. Then, you style the image with CSS/or by using the HTML "height" attribute to make it 500 pixels high.


1 Answers

I'd guess it's because you're not waiting for the image to load - try this:

var newimage = new Image(); newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg';  newimage.onload = function() {     var width = this.naturalWidth;     alert(width); } 
like image 109
Greg Avatar answered Sep 20 '22 19:09

Greg