js判断图片链接是否有效

 

let checkImgExists = function(url) {
      return new Promise(function(resolve, reject) {
        let ImgObj = new Image();
        ImgObj.src = url;
        ImgObj.onload = function(res) {
          resolve(res);
        }
        ImgObj.onerror = function(err) {
          reject(err)
        }
      })
    }

    checkImgExists('//www.baidu.com/img/flexible/logo/pc/result.png').then(()=>{
      //success callback
      console.log('有效链接')
    }).catch(()=>{
      //fail callback
      console.log('无效链接')
    })

  

posted @ 2023-01-30 14:58  PromiseOne  阅读(721)  评论(0编辑  收藏  举报