如何判断图像是否已正确加载
有没有办法在事后判断图像(使用 标签放置,而不是通过 JS)是否已正确加载到页面中?我有一个头像库,有时第三方图像服务器最终会提供 404。我可以更改服务器端代码以使用
onerror="showGenericHeadshot()"
,但是我真的想避免更改服务器端代码。最终,我想确定图像是否丢失或损坏,并将其替换为通用的“图像未找到”图形。我尝试过的事情:
Image.prototype.onerror = showGenericHeadshot
- 不适用于标签
$('img[src*= thirdpartyserver.com]).error(showGenericHeadshot)
-- 在 IE 中不起作用$('img[src*=thirdpartyserver.com]).css('backgroundImage','url(replacementimage. gif)')
-- 有效,但仍然无法消除 IE 中损坏的图像图标
Is there a way to tell, after the fact, whether an image (placed with the <img>
tag, not via JS) has loaded correctly into a page? I have a gallery of head shots, and occasionally the third-party image server ends up serving up a 404. I can change the server-side code to use an onerror="showGenericHeadshot()"
, but I really want to avoid making changes to server-side code. Ultimately, I want to determine if an image is missing or broken and replace it with a generic "Image Not Found" graphic. Things I've tried:
Image.prototype.onerror = showGenericHeadshot
-- doesn't work for<img>
tags$('img[src*=thirdpartyserver.com]).error(showGenericHeadshot)
-- doesn't work in IE$('img[src*=thirdpartyserver.com]).css('backgroundImage','url(replacementimage.gif)')
-- works, but still doesn't get rid of the broken image icon in IE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了避免竞争情况,请执行以下操作
to avoid race condition do as below
不幸的是,我无法接受@TJ Crowder 和@Praveen 的出色答案,尽管两者都执行了所需的图像替换。 @Praveen 的答案需要更改 HTML(在这种情况下,我应该挂钩
标记自己的
error=""
事件属性。并通过以下判断网络活动,看起来如果您尝试使用在同一页面中刚刚进行 404 处理的图像的 url 创建新图像,该请求实际上会再次发送,图像服务器失败的部分原因是。至少部分是我们的流量;所以我真的必须尽我所能来减少请求,否则问题只会变得更糟。@danp 对我的问题的评论中提到的 SO 问题实际上已经为我提供了答案,尽管它是我无法确认它是否适用于 IE 7 和 8、FF 和 webkit 浏览器,所以我有一个
try/catch;
在那里处理任何异常。最糟糕的情况是不会发生图像替换,这与现在不执行任何操作的情况没有什么不同,我正在使用的实现如下:Unfortunately, I'm not able to accept either @TJ Crowder's nor @Praveen's excellent answers, though both do perform the desired image-replacement. @Praveen's answer would require a change to the HTML (in which case I should just hook into the
<img>
tag's ownerror=""
event attribute. And judging by network activity, it look like if you try to create a new image using the url of an image that just 404ed in the same page, the request actually does get sent a second time. Part of the reason the image server is failing is, at least partly, our traffic; so I really have to do everything I can to keep requests down or the problem will only get worse..The SO question referred to in @danp's comment to my question actually had the answer for me, though it was not the accepted answer there. I'm able to confirm that it works with IE 7 & 8, FF and webkit browsers. I'm doubtful it will work with older browsers, so I've got a
try/catch
in there to handle any exceptions. The worse case will be that no image-replacement happens, which is no different from what happens now without doing anything. The implementation I'm using is below:替代文本就足够了吗?如果是这样,您可以使用 img 标签的 alt 属性。
Would an alternate text be sufficient? If so you can use the alt attribute of the img tag.
我想我已经明白了:当加载 DOM 时(或者甚至在
window.load
事件上)——毕竟,您希望在所有图像都完整时执行此操作获取),您可以通过创建一个新的img
元素,挂钩其load
和error
事件来追溯检查图像是否正常,然后循环抓取每个爆头的src
。类似于下面的代码(实时示例)。该代码只是匆忙完成的,它不是生产质量的 - 例如,如果您没有收到load
或error
,您可能需要一个超时,你假设错误。 (您可能必须替换检查器图像才能可靠地处理该问题。)此技术假设重用
src
不会不重新加载图像,我认为这是一个相当好的方法。可靠的假设(这当然是一个容易测试的假设),因为这种技术已永远用于预缓存图像。我已经在 Linux 的 Chrome、Firefox 和 Opera 以及 Windows 的 IE6(是的,确实如此)和 IE8 上测试了以下内容。做了一份款待。
实例
I think I've got it: When the DOM is loaded (or even on the
window.load
event — after all, you want to do this when all images are as complete as they're going to get), you can retroactively check that the images are okay by creating one newimg
element, hooking itsload
anderror
events, and then cycling through grabbing thesrc
from each of your headshots. Something like the code below (live example). That code was just dashed off, it's not production quality — for instance, you'll probably want a timeout after which if you haven't received eitherload
orerror
, you assume error. (You'll probably have to replace your checker image to handle that reliably.)This technique assumes that reusing a
src
does not reload the image, which I think is a fairly reliable assumption (it is certainly an easily testable one) because this technique has been used for precaching images forever.I've tested the below on Chrome, Firefox, and Opera for Linux as well as IE6 (yes, really) and IE8 for Windows. Worked a treat.
Live example