Skip to content

Instantly share code, notes, and snippets.

@kalepail
Last active August 29, 2015 14:08
Show Gist options
  • Save kalepail/12821f09cf354fef466e to your computer and use it in GitHub Desktop.
Save kalepail/12821f09cf354fef466e to your computer and use it in GitHub Desktop.
Get an image's natural width and height
(function($){ var props = ['Width', 'Height'], prop;
while (prop = props.pop()) {
(function (natural, prop) {
$.fn[natural] = (natural in new Image()) ?
function () {
return this[0][natural];
} :
function () {
var
node = this[0],
img,
value;
if (node.tagName.toLowerCase() === 'img') {
img = new Image();
img.src = node.src,
value = img[prop];
}
return value;
}
}('natural' + prop, prop.toLowerCase()));
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment