简体   繁体   中英

Get the final URL after (lots of) redirection with Javascript

I want to get the final URL after the redirection with just Javascript.

For example I have the url http://example.com/testClient , this redirects to different places with hashes and query parameters based on client cookies. I want to get the final URL with Javascript only.

The URL redirects to different location, with different hash and different query parameter on different clients based on cookies, user-agent, platform, etc.

This is similar to get the original (expanded) URL of a bit.ly link with Javascript.

Any suggestions on how to do this??

You could use an iframe like in this example: http://jsfiddle.net/BaliBalo/qsX6U/

iframe.addEventListener('load', function(){
    console.log(iframe.contentWindow.location.href);
}, false);

BUT there is an issue: domain of the final URL has to be the same as the one the script is running on, otherwise it will raise a cross-domain error and contentWindow properties would not be accessible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM