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

前端 未结 5 1059
既然无缘
既然无缘 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:30

    I don't believe it's possible to detect a redirect with JavaScript without some previous knowledge of the image properties. The src property of the current DOM will remain the same regardless of any redirect, so that option is out.

    I would suggest that you create a proxy script on your server that acts as a go between. For example, your image tag would look something like (with suitable URL encoding, of course):

    
    

    Your proxy script would inspect the headers for redirects before serving the image. It isn't fool proof - but it could allow you to, for example, cache the image on first load so you don't get bandwidth limit errors from the other machine.

    If it is just for your own purposes, you might be able to use an ActiveXObject within Internet Explorer to inspect the image in more detail. Can't help you on the specifics though.

    I assume that you're permitted to hotlink/cache the image with the permission of the other party.

提交回复
热议问题