可以使用Javascript来检测重定向的图像SRC(在任何流行的浏览器中)? [英] Can Javascript be used to detect a redirected image SRC (in any popular browser)?

查看:73
本文介绍了可以使用Javascript来检测重定向的图像SRC(在任何流行的浏览器中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 浏览器加载< img src = http://example.net/lolcat.png>

  2. example.net redirects 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);

在这种情况下,返回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); //图像存在并返回240

var b = new Image();
b.src ='http://farm3.static.flickr.com/2560/4098180849_729ef4f6ef_m.jpg';
console.log(b.width); //图像不存在(重新直接)并返回0


I'm guessing nope, but you never know.

  1. Browser loads <img src="http://example.net/lolcat.png">
  2. example.net redirects lolcat.png to http://static.example.net/bandwidth-exceeded.gif

Can I detect this?

Cross-browser isn't important, just looking for an option that will work in any 1 popular browser.

So far my best option is knowing the size of the placeholder image, getting a "maybe" by looking at the image size onload (in browsers that support onload events in image tags), and making a quick XHR request notifying the backend that we've got a maybe.

解决方案

I checked with the following code:

var a=new Image(); a.src='http://example.com/image.png';
alert( a.width );

which in this case returns 0 since the image doesn't exist.

On link text I found quite a few properties that might be worth checking out. And if that fails you might want to look into jQuery and AJAX.

Edit:
tested on 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

这篇关于可以使用Javascript来检测重定向的图像SRC(在任何流行的浏览器中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆