获取远程图片尺寸

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; visibility:hidden;');
  document.body.appendChild(img);
  img.onload = img.onerror = img.onreadystatechange = function () {
    if (img && img.readyState && img.readyState != 'loaded' && img.readyState != 'complete') return;
    img.onload = img.onreadystatechange = img.onerror = null;
    callback.call({"width": img.width, "height": img.height}, null);
    img.parentNode.removeChild(img);
    img = null;
  };
}

 

getImageSize({
  img: 'http://www.baidu.com/img/baidu_sylogo1.gif',
  oncomplete: function () {
    alert('宽度' + this.width + ', ' + ' 高度:' + this.height);
  }
});

posted @ 2012-11-08 16:31  baiezone  阅读(203)  评论(0编辑  收藏  举报