Is it possible to view 302 redirects using javascript?

Is there any way with Javascript to track the URL changed by redirecting 302 on the elements on the page?

I have some script on a remote server on which I do not control, is loaded via a script tag, which can, under certain circumstances, immediately return a 302 redirect to another location.

Is it possible to observe this redirection and receive notification of a new location, take various actions depending on the loaded URI?

+4
source share
2 answers

There seems to be no general wisdom here, but if you want to test it, try loading the script using jQuery ajax:

var redirected = false; $.ajax({ url: url, dataType: "script", statusCode: { 302: function() { redirected = true; } }, success: function () { if (redirected) { // something } else { // something else } } }); 

note that the script will already be running by the time the success function is received, so it can be useless even if it works.

+1
source

I do not think so. The draft W3C specification says that XMLHttpRequest will transparently follow in redirecting, and you don’t even use something that gives you such visibility in the process, when you simply add a script element to the page (hell, you are lucky to get events from it load / onreadystate ).

0
source

All Articles