0

I need to check if an external URL redirects and if it does, get the new URL.

I tried doing this:

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', 'http://example.com/test', true);
xhr.send();

from this post: detecting a redirect with javascript - how?

but it gets blocked by CORS policy. Is there any way around that?

Silveriac
  • 11
  • 4
  • CORS is handled on the server-side, to bypass it you should add a special header to the xhr request. Header: Access-Control-Allow-Origin: * – Eran Peled Dec 22 '22 at 08:01

0 Answers0