Obtaining URL of new opened webpage using JavaScript
Using ja开发者_StackOverflow社区vascript, one of the things I need to do is when a button is clicked on my index page, a new webpage (new window) is opened. This new webpage redirects to another webpage and with the setTimeout() function, I close the new window and focus back on the original index. My question is if there is a way to extract the URL of the redirected webpage so I can use it in my original index.
I tried,
winRef = window.open(url+param);
winRef.focus();
loc = window.location;
setTimeout("winRef.close()", 3000);
but this obtains the URL of my original index page (not the redirected page I need). Is there a way to do this? Thanks.
Surely you need loc = winRef.location;
, rather than loc = window.location;
, assuming winRef
is the window you are opening. You also may need to do this inside the timeout, because the redirection won't have happened straight away.
You will only be able to do this if the redirected page is on the same domain as the page you are on. If it redirects to another domain, security will prevent you seeing the new URL. If you have control of the page that is being redirected to, you could cause it to signal to the original page by changing it's hash.
One 'out of the box' approach to getting the url would be to open the URL on the server side and basically scrape / spider it and record where the url is redirecting. You could do this behind the scenes while the client is navigating away to that url or just do it once and record the result.
This may not be the easiest solution but what you want to accomplish, shouldn't be possible in javascript (again, assuming its on a different domain)
精彩评论