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

前端 未结 5 1095
既然无缘
既然无缘 2020-12-05 18:46

I\'m guessing nope, but you never know.

  1. Browser loads
  2. example.net redirects lolca
5条回答
  •  甜味超标
    2020-12-05 19:33

    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
    

提交回复
热议问题