2010-08-01 76 views
64

我想檢查圖像是否存在使用jQuery。如何檢查圖像是否存在給定的網址?

比如我如何檢查該圖像是否

http://www.google.com/images/srpr/nav_logo14.png 

的支票必須給我一個200或狀態OK

-------------- edited- ------------------

var imgsrc = $(this).attr('src'); 
var imgcheck = imgsrc.width; 


if (imgcheck==0) { 
alert("You have a zero size image"); 
} else { //do rest of code } 

感謝 讓

+1

您完全有權發佈並接受對自己問題的回答。 – sje397 2010-08-01 11:16:38

+1

@ sje397什麼!... – X10nD 2010-08-01 11:18:42

+2

是的。檢查常見問題。 – sje397 2010-08-01 11:47:02

回答

73

使用error的處理程序是這樣的:

$('#image_id').error(function() { 
    alert('Image does not exist !!'); 
}); 

如果圖像不能被加載(例如,因爲它不存在於所提供的URL)時,顯示警報:

更新:

我認爲使用:

$.ajax({url:'somefile.dat',type:'HEAD',error:do_something}); 

就足以來檢查404

更多閱讀:

更新2:

您的代碼應該是這樣的:

$(this).error(function() { 
    alert('Image does not exist !!'); 
}); 

無需這些線路,這將不會檢查遠程文件反正存在:

var imgcheck = imgsrc.width;  

if (imgcheck==0) { 
    alert("You have a zero size image"); 
} else { 
    //execute the rest of code here 
} 
+0

@sAc我沒有圖像的ID,只是想檢查文件是否存在,否則繼續執行其餘的代碼 – X10nD 2010-08-01 10:45:52

+0

@Jean:請在你的問題中發佈你的代碼,看看你有什麼或如何你需要這個。 – Sarfraz 2010-08-01 10:48:52

+0

如果你看看這個網址http://www.google.com/images/srpr/nav_logo14.png,如果圖像存在,它必須給我的大小,或編輯狀態200/ok – X10nD 2010-08-01 10:53:33

2

here

// when the DOM is ready 
$(function() { 
    var img = new Image(); 
    // wrap our new image in jQuery, then: 
    $(img) 
    // once the image has loaded, execute this code 
    .load(function() { 
     // set the image hidden by default  
     $(this).hide(); 
     // with the holding div #loader, apply: 
     $('#loader') 
     // remove the loading class (so no background spinner), 
     .removeClass('loading') 
     // then insert our image 
     .append(this); 
     // fade our image in to create a nice effect 
     $(this).fadeIn(); 
    }) 
    // if there was an error loading the image, react accordingly 
    .error(function() { 
     // notify the user that the image could not be loaded 
    }) 
    // *finally*, set the src attribute of the new image to our image 
    .attr('src', 'images/headshot.jpg'); 
}); 
+1

編輯的問題................ – X10nD 2010-08-01 11:03:37

9

,如果它不存在負載的默認圖像或處理錯誤

$('img[id$=imgurl]').load(imgurl, function(response, status, xhr) { 
    if (status == "error") 
     $(this).attr('src', 'images/DEFAULT.JPG'); 
    else 
     $(this).attr('src', imgurl); 
    }); 
27
$.ajax({ 
    url:'http://www.example.com/somefile.ext', 
    type:'HEAD', 
    error: function(){ 
      //do something depressing 
    }, 
    success: function(){ 
      //do something cheerful :) 
    } 
}); 

來自:http://www.ambitionlab.com/how-to-check-if-a-file-exists-using-jquery-2010-01-06

+11

警告:您將收到錯誤消息:[XMLHttpRequest無法加載http://not.on.your.domain.com/someimg.jpg。在請求的資源上沒有'Access-Control-Allow-Origin'頭部。]因此,如果圖像位於服務器上,這只是一個不錯的選擇。 – Twelve24 2014-10-02 00:10:02

4

使用案例

$('#myImg').safeUrl({wanted:"http://example/nature.png",rm:"/myproject/images/anonym.png"}); 

API:

$.fn.safeUrl=function(args){ 
    var that=this; 
    if($(that).attr('data-safeurl') && $(that).attr('data-safeurl') === 'found'){ 
     return that; 
    }else{ 
     $.ajax({ 
    url:args.wanted, 
    type:'HEAD', 
    error: 
     function(){ 
      $(that).attr('src',args.rm) 
     }, 
    success: 
     function(){ 
      $(that).attr('src',args.wanted) 
      $(that).attr('data-safeurl','found'); 
     } 
     }); 
    } 


return that; 
}; 

注:rm在這裏是指風險同治。


另一種使用情況:

$('#myImg').safeUrl({wanted:"http://example/1.png",rm:"http://example/2.png"}) 
.safeUrl({wanted:"http://example/2.png",rm:"http://example/3.png"}); 
  • 'http://example/1.png':如果不存在 'http://example/2.png'

  • 'http://example/2.png':如果不存在 'http://example/3.png'

相關問題