87,925
社区成员
发帖
与我相关
我的任务
分享
<img src="1.gif" onerror="doHide(this)" onload="this.style.display='inline'" width=348 height=446>
<script>
function doHide(e){
alert(1);
e.style.display = "none";
setTimeout(function(){e.src = "1.gif?tmp="+Math.random();}, 4000);
}
</script>
利用image对象的onerror事件来判断,出错则更换image对象的src为默认图片的URL。<p>第一种情况:图片存在,正常显示<br />
<xmp>
<img src="http://www.iecn.net/images/logo_home.gif"
onerror="javascript:this.src='http://www.cnlei.org/BLOG/styles/style2007/images/logo.gif'" />
</xmp>
<img src="http://www.iecn.net/images/logo_home.gif" onerror="javascript:this.src='http://www.cnlei.org/BLOG/styles/style2007/images/logo.gif'" /></p>
<p>第二种情况:图片不存在,显示默认图片<br />
<xmp>
<img src="http://www.iecn.net/images/logo_homeAAA.gif"
onerror="javascript:this.src='http://www.cnlei.org/BLOG/styles/style2007/images/logo.gif'" />
</xmp>
<img src="http://www.iecn.net/images/logo_homeAAA.gif" onerror="javascript:this.src='http://www.cnlei.org/BLOG/styles/style2007/images/logo.gif'" /></p>
判断远程图片是否存在的asp技巧[ASP代码]
function CheckURL(byval A_strUrl)
set XMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
XMLHTTP.open "HEAD",A_strUrl,false
XMLHTTP.send()
CheckURL=(XMLHTTP.status=200)
set XMLHTTP = nothing
end function
Dim imgurl
imgurl="UploadFiles/2007829144940734.gif"
if CheckURL(imgurl) then
response.write "图片存在"
else
response.write "图片不存在"
end if
判断远程图片是否存在[js代码]
{
var oReq = new ActiveXObject("Microsoft.XMLHTTP")
oReq.open("Get","UploadFiles/2007829144941621.gif",false);
oReq.send();
//alert(oReq.status)
if(oReq.status==404)
alert('不存在');
else
alert("存在")
}
<img src="adf.jpg" onload="alert('图片存在');" onerror="alert('图片不存在');">