首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何检查图片是否存在给定的url?

如何检查图片是否存在给定的url?
EN

Stack Overflow用户
提问于 2010-08-01 18:34:08
回答 5查看 134.5K关注 0票数 79

我想使用jquery检查是否存在镜像。

例如,我如何检查此图像是否存在

代码语言:javascript
复制
http://www.google.com/images/srpr/nav_logo14.png 

检查结果必须是200或状态ok

代码语言:javascript
复制
var imgsrc = $(this).attr('src');
var imgcheck = imgsrc.width;


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

谢谢,吉恩

EN

回答 5

Stack Overflow用户

发布于 2012-08-02 18:04:42

代码语言:javascript
复制
$.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

票数 34
EN

Stack Overflow用户

发布于 2012-02-15 07:05:59

如果它不存在,则加载默认图像或处理错误

代码语言:javascript
复制
$('img[id$=imgurl]').load(imgurl, function(response, status, xhr) {
    if (status == "error") 
        $(this).attr('src', 'images/DEFAULT.JPG');
    else
        $(this).attr('src', imgurl);
    });
票数 13
EN

Stack Overflow用户

发布于 2010-08-01 18:40:16

来自here

代码语言:javascript
复制
// 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');
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3381663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档