Your code as is won't work, unless the image is cached - and even then 
it'll be hit an miss.  The img.src = "1.jpg" line is an asynchronous 
call.  WHILE the image is loading the next line is executed.  Seeing as 
the image probably didn't load in a microsecond or less, the 
width/height values would be undefined.

I've done this in the past with an iframe.  loaded my image into an 
iframe, and had the body's onload method then figure out the 
height/width and report that back to the main page.  That's kinda 
"hacky" though.

I haven't done this with jQuery yet, but think you can do something like 
this:

var w;
var h;
$("#myimg").attr("src", "1.jpg").ready(function () {
   w = $(this).width();
   h = $(this).height();
});

I don't think the .ready() method will work on a variable in memory 
only.  So you'll have to put an image tag on your page (manually, or via 
the .html() method), and probably set it's display to hidden.  (hmm... I 
think a hidden image doesn't have a height/width though).

Anyways, I'm speculating here.  Hope it helps.

Shawn

BAlex wrote:
> Is JavaScript:
> 
> var img = new Image();
> img.src = "1.jpg";
> var width = img.width;
> var height = img.height;
> 
> It is necessary for preliminary loading image and, the main thing, for
> preliminary definition width and height.
> 
> How same to represent on jQuery?
> 
> In advance thanks,
> Alexander
> 

Reply via email to