长宽获取(此方法只在网络加载图片,且未知其大小前需要控制时使用)
image.src=PATH;//图片路径,以下都是判断浏览器,然后获取大小,也就是我们常做的预加载图片。 var userAgent = navigator.userAgent.toLowerCase(); var kmki=""; if(navigator.userAgent.indexOf("MSIE")>0){ kmki="0"; } console.log(userAgent); if(kmki=="0"){ if($.browser.version==6.0){ image.onreadystatechange = function () { if (image.readyState == "complete"){ //var logw=image.width,logh=image.height; ak(image.width,image.height)
//IE6,7,8获取图片质量大小开源用image.fileSize } }; }else{ ie7imagetime = window.setInterval(function(){ var rs = image.readyState; if(rs=="complete"){ window.clearInterval(ie7imagetime); //var logw=image.width,logh=image.height; ak(image.width,image.height)
//IE6,7,8获取图片质量大小开源用image.fileSize }else{ return; } },200); } }else{ image.onload = function () { if (image.complete == true){ //var logw=image.width,logh=image.height; ak(image.width,image.height) } }; }
获取质量大小
<input type="button" value="GET Size" onclick="getFileSize(document.getElementById('xx'))" /> <img src="a.jpg" id="xx" alt="" /> <script> function getFileSize(o) { x = window.XMLHttpRequest ? new window.XMLHttpRequest : new ActiveXObject("MSxml2.XMLHTTP"); x.open("HEAD", o.src, false); x.send(); alert("大小:" + x.getResponseHeader("Content-Length")); } </script>
此方法需要开ActiveX控件