Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  6] [ 2]  / answers: 1 / hits: 40748  / 12 Years ago, fri, march 9, 2012, 12:00:00

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.


More From » redirect

 Answers
18

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.


[#86951] Thursday, March 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dequant

Total Points: 88
Total Questions: 99
Total Answers: 95

Location: Ukraine
Member since Sun, Dec 13, 2020
4 Years ago
dequant questions
;