利用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("存在") }