Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

dom - Can Javascript be used to detect a redirected image SRC (in any popular browser)?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...