admin管理员组文章数量:1327675
I need to know the image size when it loads from an extern URL because the project needs to resize the image div.
I need to do something like this:
<img id="imglegend'+layername+'" src="url_to_an_extern_host" />
And, using Javascript and JQuery:
$('#imglegend'+layername).ready(function(){
var h = $('#imglegend'+layername);
// Resize image div container
});
But this didn't work. Is it possible to do?
Thanks in advance!
I need to know the image size when it loads from an extern URL because the project needs to resize the image div.
I need to do something like this:
<img id="imglegend'+layername+'" src="url_to_an_extern_host" />
And, using Javascript and JQuery:
$('#imglegend'+layername).ready(function(){
var h = $('#imglegend'+layername);
// Resize image div container
});
But this didn't work. Is it possible to do?
Thanks in advance!
Share Improve this question asked Jan 17, 2011 at 13:07 Fran VeronaFran Verona 5,4766 gold badges48 silver badges87 bronze badges 2-
2
That should be handled by styling not programmatically.
<img>
by default resizes to image size so your other formatting is not correct. – Aliostad Commented Jan 17, 2011 at 13:13 - But this project loads images dinamically, and I need to know image size dinamically too. Thanks Aliostad! – Fran Verona Commented Jan 17, 2011 at 13:26
2 Answers
Reset to default 5$('#imglegend').load(function(){
var w = $(this).width();
var h = $(this).height();
alert(w); alert(h);
}).error(function (){
$(this).remove();//remove image if it fails to load// or what ever u want
})
Images don't have a ready
event. They do however have a load
event:
$('#imglegend'+layername).load(function(){
alert(this.width);
});
EDIT: BTW, you need to make sure that the image isn't loaded before you attach the event handler. You'll wither need to assign the src
in your script after assigning the handler instead of in your HTML, or assign the handler in the onload
HTML instead.
本文标签: jqueryGet image size when it loads from an extern URL in JavascriptStack Overflow
版权声明:本文标题:jquery - Get image size when it loads from an extern URL in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223483a2435601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论