jQuery/JavaScript to replace broken images

后端 未结 30 2600
我寻月下人不归
我寻月下人不归 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:04

    You can use GitHub's own fetch for this:

    Frontend: https://github.com/github/fetch
    or for Backend, a Node.js version: https://github.com/bitinn/node-fetch

    fetch(url)
      .then(function(res) {
        if (res.status == '200') {
          return image;
        } else {
          return placeholder;
        }
      }
    

    Edit: This method is going to replace XHR and supposedly already has been in Chrome. To anyone reading this in the future, you may not need the aforementioned library included.

提交回复
热议问题