Inbal
Inbal

Reputation: 919

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.

Upvotes: 7

Views: 36964

Answers (8)

Jafar Ashkany
Jafar Ashkany

Reputation: 56

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

Upvotes: 0

Adam Sparks
Adam Sparks

Reputation: 538

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

Upvotes: 1

erionpc
erionpc

Reputation: 398

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"

Upvotes: 0

Per Kristian
Per Kristian

Reputation: 795

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

Upvotes: 0

Johan Hörnqvist
Johan Hörnqvist

Reputation: 159

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

Upvotes: 11

Walter Brand
Walter Brand

Reputation: 689

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;

Upvotes: 2

Dillen Meijboom
Dillen Meijboom

Reputation: 964

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.

Upvotes: 2

Jacco
Jacco

Reputation: 3272

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.

Upvotes: 1

Related Questions