Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the redirect url using javascript?

I'm having trouble showing urls with redirect with an automation tool i'm using so I'm thinking of checking the url for redirects first before opening the popup.

Is it possible to get the redirect URL using javascript (no jquery or other js framework as this is not supported by the tool)?

Thanks!

like image 836
Jose Avatar asked Jul 28 '12 04:07

Jose


People also ask

What syntax do you use to redirect a URL with JavaScript?

href = "https://www.example.com"; Simply insert your target URL that you want to redirect to in the above code.

How do I redirect a URL to another URL in HTML?

To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page. The <head> section of an HTML document contains metadata that is useful for the browser, but invisible to users viewing the page.


1 Answers

In theory, yes. In practice, probably not.

In theory you could do an XHR head request to the URL, then check the location header to see if what you asked for is what you ended up getting. XHR will automatically follow 301/302 redirects, so there's no use checking the status code - Is it possible for XHR HEAD requests to not follow redirects (301 302)

In practice, unless the domain you're asking is your domain, or has a Access-Control-Allow-Origin header (I think it's header), the request will fail due to cross site request restrictions. HEAD XMLHttpRequest on Chromium

If you've got access to the server running your tool, you should be able to whip up a "proxy" page that issues the HEAD request, then you can XHR call your server page and it would return where the HEAD request said you'd end up.

like image 150
Dan F Avatar answered Sep 21 '22 09:09

Dan F