简体   繁体   中英

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:

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

How can I get this url in javascript?

Thanks a lot,

Inbal.

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!

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).

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.

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

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

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

You have something like: http://www.example.com/something/index.html You want: 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

I normally create a "settings.js" file in which I store a collection of settings for the application. 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,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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