jQuery/JavaScript to replace broken images

后端 未结 30 2555
我寻月下人不归
我寻月下人不归 2020-11-21 05:50

I have a web page that includes a bunch of images. Sometimes the image isn\'t available, so a broken image is displayed in the client\'s browser.

How do I use jQuery

相关标签:
30条回答
  • 2020-11-21 06:06

    I got the same problem. This code works well on my case.

    // Replace broken images by a default img
    $('img').each(function(){
        if($(this).attr('src') === ''){
          this.src = '/default_feature_image.png';
        }
    });
    
    0 讨论(0)
  • 2020-11-21 06:07

    I found this post while looking at this other SO post. Below is a copy of the answer I gave there.

    I know this is an old thread, but React has become popular and, perhaps, someone using React will come here looking for an answer to the same problem.

    So, if you are using React, you can do something like the below, which was an answer original provided by Ben Alpert of the React team here

    getInitialState: function(event) {
        return {image: "http://example.com/primary_image.jpg"};
    },
    handleError: function(event) {
        this.setState({image: "http://example.com/failover_image.jpg"});
    },
    render: function() {
        return (
            <img onError={this.handleError} src={src} />;
        );
    }
    
    0 讨论(0)
  • 2020-11-21 06:07

    I created a fiddle to replace the broken image using "onerror" event. This may help you.

        //the placeholder image url
        var defaultUrl = "url('https://sadasd/image02.png')";
    
        $('div').each(function(index, item) {
          var currentUrl = $(item).css("background-image").replace(/^url\(['"](.+)['"]\)/, '$1');
          $('<img>', {
            src: currentUrl
          }).on("error", function(e) {
            $this = $(this);
            $this.css({
              "background-image": defaultUrl
            })
            e.target.remove()
          }.bind(this))
        })
    
    0 讨论(0)
  • 2020-11-21 06:07

    I think I have a more elegant way with event delegation and event capturing on window's error even when the backup image fail to load.

    img {
      width: 100px;
      height: 100px;
    }
    <script>
      window.addEventListener('error', windowErrorCb, {
        capture: true
      }, true)
    
      function windowErrorCb(event) {
        let target = event.target
        let isImg = target.tagName.toLowerCase() === 'img'
        if (isImg) {
          imgErrorCb()
          return
        }
    
        function imgErrorCb() {
          let isImgErrorHandled = target.hasAttribute('data-src-error')
          if (!isImgErrorHandled) {
            target.setAttribute('data-src-error', 'handled')
            target.src = 'backup.png'
          } else {
            //anything you want to do
            console.log(target.alt, 'both origin and backup image fail to load!');
          }
        }
      }
    </script>
    <img id="img" src="error1.png" alt="error1">
    <img id="img" src="error2.png" alt="error2">
    <img id="img" src="https://i.stack.imgur.com/ZXCE2.jpg" alt="avatar">

    The point is :

    1. Put the code in the head and executed as the first inline script. So, it will listen the errors happen after the script.

    2. Use event capturing to catch the errors, especially for those events which don't bubble.

    3. Use event delegation which avoids binding events on each image.

    4. Give the error img element an attribute after giving them a backup.png to avoid disappearance of the backup.png and subsequent infinite loop like below:

    img error->backup.png->error->backup.png->error->,,,,,

    0 讨论(0)
  • 2020-11-21 06:08

    Here is a quick-and-dirty way to replace all the broken images, and there is no need to change the HTML code ;)

    codepen example

        $("img").each(function(){
            var img = $(this);
            var image = new Image();
            image.src = $(img).attr("src");
            var no_image = "https://dummyimage.com/100x100/7080b5/000000&text=No+image";
            if (image.naturalWidth == 0 || image.readyState == 'uninitialized'){
                $(img).unbind("error").attr("src", no_image).css({
                    height: $(img).css("height"),
                    width: $(img).css("width"),
                });
            }
      });
    
    0 讨论(0)
  • 2020-11-21 06:08

    Sometimes using the error event is not feasible, e.g. because you're trying to do something on a page that’s already loaded, such as when you’re running code via the console, a bookmarklet, or a script loaded asynchronously. In that case, checking that img.naturalWidth and img.naturalHeight are 0 seems to do the trick.

    For example, here's a snippet to reload all broken images from the console:

    $$("img").forEach(img => {
      if (!img.naturalWidth && !img.naturalHeight) {
        img.src = img.src;
      }
    }
    
    0 讨论(0)
提交回复
热议问题