在 javascript 中获取根网站 url 以进行重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14135479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:48:38  来源:igfitidea点击:

Get root website url in javascript for redirect

javascriptasp.netwindow.location

提问by Inbal

I want to redirect to Login page from every page in my website after session timeout. I try to set window.location to the login page:

我想在会话超时后从我网站的每个页面重定向到登录页面。我尝试将 window.location 设置为登录页面:

var ParentUrl = encodeURIComponent(window.parent.location.href);
document.location.href = "~/Login.appx?ReturnUrl=" + ParentUrl;

but "~" seems to be unsupported. my Login page is located under my root folder.

但“~”似乎不受支持。我的登录页面位于我的根文件夹下。

for example: *http://server/website/*Login.aspx

例如: * http://server/website/*Login.aspx

How can I get this url in javascript?

我怎样才能在 javascript 中获取这个 url?

Thanks a lot,

非常感谢,

Inbal.

英巴尔。

回答by Johan H?rnqvist

I would use the window.location.origin. It will return the first part for you and after that just Uri encode the parent url and it's done!

我会使用window.location.origin。它会为你返回第一部分,然后只是 Uri 编码父 URL 就完成了!

var parentUrl = encodeURIComponent(window.location.href),
    loginUrl = window.location.origin+"/Login.appx?ReturnUrl=" + parentUrl;

window.location.href = loginUrl;

A small tip for cross browser functionality is to use window.location. It's read/write on all compliant browsers. While document.location is read only in some (ie).

跨浏览器功能的一个小技巧是使用 window.location。它在所有兼容的浏览器上都可以读/写。而 document.location 在某些(即)中是只读的。

回答by Walter Brand

Why use ~ ? At first glance I would say removing it solves your problem. Like this.

为什么使用 ~ ?乍一看,我会说删除它可以解决您的问题。像这样。

   document.location.href = "/Login.appx?ReturnUrl=" + ParentUrl;

[EDIT] responding to first comment...

[编辑] 回应第一条评论...

I believe this could do the trick:

我相信这可以解决问题:

function getLoginPage() {
    var urlParts = document.location.href.split("/");
    return "/" + urlParts[2] + "/" + urlParts[3] + "/Login.aspx";
}

document.location.href = getLoginPage() + "?ReturnUrl=" + ParentUrl;

回答by Dillen Meijboom

function getURL() { 
    var arr = window.location.href.split("/"); 
    delete arr[arr.length - 1]; 
    return arr.join("/"); 
}

You can use it like this:

你可以这样使用它:

document.location.href = getURL() + "Login.appx?ReturnUrl=";

The difference between my function and the first answer is that a "/" will redirect to server/page and my code will redirect (in your example URL) to server/website/page.

我的函数和第一个答案之间的区别在于“/”将重定向到服务器/页面,而我的代码将(在您的示例 URL 中)重定向到服务器/网站/页面。

回答by Jacco

The "/website" part is typically server side information. No way JavaScript can determine this by itself.

“/website”部分通常是服务器端信息。JavaScript 无法自行确定这一点。

So you will have to pass this from the server to the client. You might as well pass "http://server/website" at once then.

因此,您必须将其从服务器传递给客户端。您不妨立即通过“http://server/website”。

回答by Per Kristian

This function will return the root (base) URL of the current url.

此函数将返回当前 url 的根 (base) URL。

You have something like: http://www.example.com/something/index.htmlYou want: http://www.example.com/something/

你有这样的东西:http: //www.example.com/something/index.html你想要:http: //www.example.com/something/

function getBaseUrl() {
    var re = new RegExp(/^.*\//);
    return re.exec(window.location.href);
}

Details here: Javascript: Get base URL or root URL

此处的详细信息:Javascript:获取基本 URL 或根 URL

回答by erionpc

I normally create a "settings.js" file in which I store a collection of settings for the application. In this case:

我通常会创建一个“settings.js”文件,在其中存储应用程序的设置集合。在这种情况下:

settings = { "root": "myAppRoot/" /*, ... */ };

settings = { "root": "myAppRoot/" /*, ... */ };

and then in the script, for example I call

然后在脚本中,例如我调用

myDynamicDiv.load("/" + settings.root + "urlPart1/urlPart2/app"

myDynamicDiv.load("/" + settings.root + "urlPart1/urlPart2/app"

回答by Adam Sparks

I modified PerKristian's answer so it would work in my situation,

我修改了 PerKristian 的答案,所以它适用于我的情况,

function getBaseUrl() {
    var re = new RegExp(/^.*\/\/[^\/]+/);
    return re.exec(window.location.href);
}

matches everything up till the first lone /

匹配一切,直到第一个孤独 /

for example

例如

http://stackoverflow.com/questions/14135479/get-root-website-url-in-javascript-for-redirect

http://stackoverflow.com/questions/14135479/get-root-website-url-in-javascript-for-redirect

will return http://stackoverflow.com

将返回 http://stackoverflow.com

回答by Jafar Ashkany

function homepage_of(url) { 
    var arr = url.replace('//','@@').split("/");
    return arr[0].replace('@@','//');
}