Is there any way to detect whether a webpage is going to redirect me to another, knowing its URL? I mean the situation when you type URL in a text field and the script examines it for 3xx redirections.
Advertisement
Answer
Yes, you can do this quite easily in Javascript. It’d look something like:
JavaScript
x
11
11
1
var xhr = new XMLHttpRequest();
2
xhr.onload = function() {
3
if (this.status < 400 && this.status >= 300) {
4
alert('this redirects to ' + this.getResponseHeader("Location"));
5
} else {
6
alert('doesn't redirect ');
7
}
8
}
9
xhr.open('HEAD', '/my/location', true);
10
xhr.send();
11
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.