With javascript, how do you get final result URL after 302 redirect on img src?

前端 未结 5 502
花落未央
花落未央 2020-12-04 02:30

If there is an img tag in a page whose final image it displays comes after a 302 redirect, is there a way with javascript to obtain what that final URL is after the redirect

5条回答
  •  眼角桃花
    2020-12-04 03:12

    I know this question is old, and was already marked answered, but another question that I was trying to answer was marked as a duplicate of this, and I don't see any indication in any of the existing answers that you can get the true URL via the HTTP header. A simple example (assuming a single image tag on your page) would be something like this...

    var req = new XMLHttpRequest();
    
    req.onreadystatechange=function() {
        if (req.readyState===4) {// && req.status===200) {
            alert("actual url: " + req.responseURL);
        }
    }
    
    req.open('GET', $('img').prop('src'), true);
    req.send();
    

提交回复
热议问题