如何使用javascript获取重定向网址? [英] How can I get the redirect url using javascript?

查看:72
本文介绍了如何使用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.

是否可以使用javascript获取重定向URL(没有jquery或其他js框架,因为该工具不支持)?

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

谢谢!

推荐答案

理论上,是的。在实践中,可能不是。

In theory, yes. In practice, probably not.

理论上你可以对URL做一个XHR头请求,然后检查位置标题,看看你要求的是你结束了什么得到。 XHR将自动遵循301/302重定向,因此检查状态代码是没有用的 - XHR HEAD请求是否有可能不遵循重定向(301 302)

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)

在实践中,除非您要求的域您的域,或者具有 Access-Control-Allow-Origin 标头(我认为它是标题),由于跨站点请求限制,请求将失败。 关于Chromium的HEAD XMLHttpRequest

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

如果你可以访问运行你的工具的服务器,你应该能够发出一个发出HEAD请求的代理页面,然后你可以XHR调用你的服务器页面,它会返回到HEAD请求说你的地方结束。

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.

这篇关于如何使用javascript获取重定向网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆