我猜不,但你永远不会知道.
<img src="http://example.net/lolcat.png">lolcat.png到http://static.example.net/bandwidth-exceeded.gif我可以检测到这个吗?
跨浏览器并不重要,只需寻找可在任何1种流行浏览器中使用的选项.
到目前为止,我最好的选择是知道占位符图像的大小,通过查看图像大小onload(在支持图像标记中的onload事件的浏览器中)得到"可能",并发出快速XHR请求通知后端我们'我有一个可能.
我检查了以下代码:
var a=new Image(); a.src='http://example.com/image.png';
alert( a.width );
Run Code Online (Sandbox Code Playgroud)
在这种情况下,由于图像不存在,因此返回0.
在链接文本中,我发现了很多可能值得一试的属性.如果失败了,你可能想看看jQuery和AJAX.
编辑:
在firefox firebug上测试
var a=new Image();
a.src='http://l.yimg.com/g/images/photo_unavailable_m.gif';
console.log(a.width); // the image exists and returns 240
var b=new Image();
b.src='http://farm3.static.flickr.com/2560/4098180849_729ef4f6ef_m.jpg';
console.log(b.width); // the image does not exist (re-direct) and returns 0
Run Code Online (Sandbox Code Playgroud)
我认为如果事先不了解图像属性,就不可能用 JavaScript 检测重定向。无论任何重定向如何,当前 DOM 的 src 属性都将保持不变,因此该选项已失效。
我建议您在服务器上创建一个代理脚本作为中间人。例如,您的图像标签看起来像这样(当然,使用合适的 URL 编码):
<img src='/proxy?src=http://example.com/image.png' />
Run Code Online (Sandbox Code Playgroud)
您的代理脚本将在提供图像之前检查重定向标头。它并不是万无一失的 - 但它可以允许您在第一次加载时缓存图像,这样您就不会收到来自其他计算机的带宽限制错误。
如果只是为了您自己的目的,您也许可以使用 Internet Explorer 中的 ActiveXObject 来更详细地检查图像。但无法帮助您了解具体情况。
我假设您可以在另一方的许可下热链接/缓存图像。