如何在javascript中获得重定向的网址[英] How to get the redirected url in javascript

本文是小编为大家收集整理的关于如何在javascript中获得重定向的网址的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<script>
function change(){
    var clientID = "hgffgh";
    var clientSecret = "fgfhgfh";
    var callbackUri = "https://login.live.com/oauth20_desktop.srf";

    var tokenUri = "https://www.box.com/api/oauth2/token";
    var authUri = "https://www.box.com/api/oauth2/authorize?"+
            "client_id="+clientID+
            "&response_type=code"+
            "&redirect_uri="+callbackUri

    //var web = window.open(authUri);
    //console.log(web.location.href); 

    var win = window.open(authUri, "windowname1", 'width=800, height=600'); 

    //alert(win)
}
</script>

Authuri打开一个新窗口.在我单击"允许的新窗口"中,我将被重定向到新窗口,如何获取最后一个重定向的URL?

推荐答案

注意:如果您至少可以在重定向URL中声明变量.


在重定向到下一页之前,只需将" redir"设置为URL中的重定向页面URL(locations.href),但如下编码:

"https://www.box.com/api/oauth2/authorize?redir=" + encodeURIComponent(location.href)

现在,要获取重定向页面,您需要从当前URL获取变量" redir".检查这个问题.因此,在获得" redir"后,您将其解码:

var redirectPage = decodeURIComponent(url);

其他推荐答案

这是不可能的. JavaScript只能从其页面中获取内容

本文地址:https://www.itbaoku.cn/post/1937765.html

问题描述

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<script>
function change(){
    var clientID = "hgffgh";
    var clientSecret = "fgfhgfh";
    var callbackUri = "https://login.live.com/oauth20_desktop.srf";

    var tokenUri = "https://www.box.com/api/oauth2/token";
    var authUri = "https://www.box.com/api/oauth2/authorize?"+
            "client_id="+clientID+
            "&response_type=code"+
            "&redirect_uri="+callbackUri

    //var web = window.open(authUri);
    //console.log(web.location.href); 

    var win = window.open(authUri, "windowname1", 'width=800, height=600'); 

    //alert(win)
}
</script>

AuthUri opens a new window. In new window when I click on allow, I will be redirected to a new window, How to get the last redirected url ?

推荐答案

NOTE: You can do that if you can at least declare variables in redirect URL.


Before you redirect to the next page, just set "redir" as the redirect page URL (locations.href) in your URL, but encoded as below:

"https://www.box.com/api/oauth2/authorize?redir=" + encodeURIComponent(location.href)

Now, to get the redirect page you need to get the variable "redir" from current URL. Check this question. So, after getting the "redir", you decode it:

var redirectPage = decodeURIComponent(url);

其他推荐答案

That is not possible. JavaScript can only get things from its page