现在的位置: 首页 > 综合 > 正文

js 获取远程图片大小(判断上传图片大小)

2013年10月09日 ⁄ 综合 ⁄ 共 818字 ⁄ 字号 评论关闭
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.onload=img.onerror=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);
              });

抱歉!评论已关闭.