3

I am trying to get a hold of a URL that another URL redirects to.

For instance, I go to http://www.example.com/redirect. Now this page redirects me to http://www.some-other-domain.com/?language=english.

How do I get the http://www.some-other-domain.com/?language=english URL using Javascript, only knowing the http://www.example.com/redirect URL?

Maeh
  • 1,774
  • 4
  • 18
  • 31
  • how are you sending the request? in ajax?? – Sunand Sep 22 '14 at 22:15
  • 1
    It seems you're not able to see the redirect response; redirects are followed transparently. However, you may be able to look at `responseURL` in the final response. See http://stackoverflow.com/questions/8056277/how-to-get-response-url-in-xmlhttprequest – Jacob Sep 22 '14 at 22:17
  • Unless the redirected site is CORS-enabled, your GET request will return an error. Most likely, you'll want to use an external server to do this work for you by making a request to that server. I tried Googling existing tools, but none of them are CORS-enabled (so you can't use them on other sites). – Pluto Sep 22 '14 at 22:55

1 Answers1

0

If your browser is set up to allow opening a new window or tab, you can do something like this:

var whndl, otherURL;  //whndl=window-handle
function prep()
{ window.name="thiswindow";
  whndl = window.open("http://www.example.com/redirect", "otherwindow");
  setTimeout("fetchURL();", 500); //half-second for page to at least partly load
  return;
}

function fetchURL()
{ otherURL = whndl.document.URL;  //should be the redirected URL
  whndl.close();  //if you don't need it open any longer
  return;
}

You might be able to hide the "otherwindow" as this is done, so nobody sees it, perhaps by using the "pop-under" trick (I confess at this time I don't know exactly what that trick is).