detecting a redirect with javascript - how? detecting a redirect with javascript - how? javascript javascript

detecting a redirect with javascript - how?


Yes, you can do this quite easily in Javascript. It'd look something like:

var xhr = new XMLHttpRequest();xhr.onload = function() {  if (this.status < 400 && this.status >= 300) {    alert('this redirects to ' + this.getResponseHeader("Location"));  } else {    alert('doesn\'t redirect ');  }}xhr.open('HEAD', '/my/location', true);xhr.send();

Unfortunately, this only works on your own server, unless you hit a server with CORS set up. If you wanted to work uniformly across any domain, you're going to have to do it server-side.


Here is simple solution for your question.

x.addEventListener('readystatechange',function(){    const url = 'https://your/request/url';    if(this.responseURL != url){        alert('redirected');    }});