tags:

views:

29

answers:

2

how would i get a image width using only the image link in jquery? can someone lead me down the right path please

Thank You,

+4  A: 
var img = new Image();
img.src = "http://your.host.here/your.image.file.png";
img.onload = function() {
  alert(this.width);
};
Pointy
Requires that the entire image be downloaded only to get it's width, but there is really no other way with pure JavaScript.
meagar
Well yes that's true; until somebody cooks up a $.psychic() plugin for jQuery we're kind of stuck with that :-)
Pointy
I was tempted to down vote two of your answers so that we would have *exactly* the same rep. I decided to go the other way though and up vote this one :-P +1
Andy E
@Andy thanks!! I'm about to have a whole bunch of real work to do so you'll probably pass me again :-)
Pointy
+3  A: 

You can do this using JavaScript, but only in an asynchronous manner, because the image would need to be downloaded before you could access its width:

function getImgSize(src, callback) { 
    var img = new Image();
    img.src = src;
    img.onload = function () { callback(this.width, this.height); }
}

Example usage

getImgSize("http://www.google.co.uk/logos/classicplus.png", function (w, h) {
    alert(w);
});
Andy E
Thank You for the quick reply
Gully