简体   繁体   English

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

[英]Get root website url in javascript for redirect

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.英巴尔。

I would use the window.location.origin.我会使用window.location.origin。 It will return the first part for you and after that just Uri encode the parent url and it's done!它会为你返回第一部分,然后只是 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.跨浏览器功能的一个小技巧是使用 window.location。 It's read/write on all compliant browsers.它在所有兼容的浏览器上都可以读/写。 While document.location is read only in some (ie).而 document.location 在某些(即)中是只读的。

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;
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 中)重定向到服务器/网站/页面。

The "/website" part is typically server side information. “/website”部分通常是服务器端信息。 No way JavaScript can determine this by itself. 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”。

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.html You 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

I normally create a "settings.js" file in which I store a collection of settings for the application.我通常会创建一个“settings.js”文件,在其中存储应用程序的设置集合。 In this case:在这种情况下:

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

and then in the script, for example I call然后在脚本中,例如我调用

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

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

will return http://stackoverflow.com将返回http://stackoverflow.com

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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