var images = document.getElementsByTagName("img"); for (var i = 0; i < images.length; i++) { var img = images[i]; if (img !== null && img.src !== null && img.src) { if (img.src.indexOf("wx.qlogo.cn") >= 0 || img.src.indexOf("thirdwx.qlogo.cn") >= 0) { avatarStatusChecker(img, function (img, status) { //失效头像 if (!status) { img.src = "/Assets/img/defaultAvatar.png"; } }); } } } function avatarStatusChecker(img, fn) { var xhr = new XMLHttpRequest(); xhr.open(‘get‘, img.src, true); xhr.send(); xhr.onreadystatechange = function (e) { if (this.readyState === 4 && this.status === 200) { var hsCode = (this.responseText.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a }, 0)); if (typeof fn === "function") { fn(img, hsCode !== -1574534679); }; } }; }