function getImageSize(cfg){
var img=document.createElement('img');
callback=cfg.oncomplete;
img.src = typeof cfg.img == 'string'?cfg.img:cfg.img.src;
img.setAttribute('style','position:absolute;dispaly:none;');
document.body.appendChild(img);
if(!+[1,]){//判断是否为IE浏览器
if(img.complete){
callback.call({"width":img.width,"height":img.height},null);
BB_removeElement(img);
}
}
img.οnlοad=img.οnerrοr=img.onreadystatechange=function(){
if(img&&img.readyState&&img.readyState!='loaded'&&img.readyState!='complete'){
return false;
}
img.onload = img.onreadystatechange = img.onerror = null;
callback.call({"width":img.width,"height":img.height},null);
img.parentNode.removeChild(img);
img=null;
};
注:在非IE浏览器下,即使是缓存的图片载入也有onload触发。而在IE下却没有,所以用complete属性。
调用方法:
getImageSize({
img:''//(图片的url)
oncomplete:function(){
alert('宽度:'+this.width+','+'高度:'+this.height);
});