2012-03-06 4 views
0

이미지 URL이 있습니다 (http://t2.gstatic.com/images?q=tbn:ANd9GcTR_RHZrrb25Cx1qGPul6PYsCnVsIqtRuJxRS1Bj0I8DPXqQx-zow). 자바 스크립트에서 이미지 존재 여부를 확인하고 싶습니다. 솔루션을 찾는 데 도움을주십시오.이미지 URL 유효성 검사

+0

가능한 중복 파일이 존재하는 경우 jQuery 또는 Javascript?] (http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascr ipt) – Curt

답변

0

내 게시물은 이전에 anothershrubery 님이 게시 한 Change image source if file exists와 (과) 거의 일치합니다.

이이

<img src="http://mydomain.com/images/image1.jpg" onerror="this.src='http://mydomain2.com/images/image1.jpg'" /> 

또는

<script language="JavaScript"><!- 
function testImage(URL) { 
var tester=new Image(); 
tester.onLoad=isGood; 
tester.onError=isBad; 
tester.src=URL; 
} 

function isGood() { 
alert('That image exists!'); 
} 

function isBad() { 
alert('That image does no exist!'); 
} 
//--></script> 
1

당신은 숨겨진 DIV에 이미지를로드하고 이미지가로드 된 경우 확인할 수 있습니다 시도 할 수 있습니다. 이것을 달성하기 위해 jQuery를 사용하는 것이 좋습니다.

로드 이벤트에

추가 읽기 : http://api.jquery.com/load-event/

HTML :

<div id="image-container" style="display: none"> 
    <img src="http://your/image/url"> 
</div> 

자바 스크립트/jQuery를 코드 :

$('img', '#image-container ').load(function() { 
     // image loaded 
    }); 
2
try { 
var img = document.createElement("img"); 
img.src ="http://t2.gstatic.com/images?q=tbn:ANd9GcTR_RHZrrb25Cx1qGPul6PYsCnVsIqtRuJxRS1Bj0I8DPXqQx-zowsd"; 

} catch(err) 
{ 
    // 
} 

if(img.height > 0) { 
//image exists 
} else { 
// image not exists 
} 
내가 확인하려면 어떻게해야 [의
관련 문제