Get root website url in javascript for redirect
Asked Answered
E

8

7

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.

Eldwin answered 3/1, 2013 at 8:42 Comment(1)
What's the content of your window.parent.location.href?Unfolded
P
11

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

Pondweed answered 18/9, 2013 at 8:53 Comment(1)
Out of curiosity, is this (window.location.origin) what's used internally when you have an anchor link with a href starting with "/"? e.g. <a href="/foo">Groundwork
M
2

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;
Muriate answered 3/1, 2013 at 8:51 Comment(1)
I tried... but If my current page is for example: server/website/Pages/Page.aspx, so I get document.location.href = server/website/Pages/Login.aspx which actually is located under: server/website/Login.aspxEldwin
H
2
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.

Hypersensitive answered 3/1, 2013 at 8:55 Comment(2)
Thanks! but it works like the first solution exactly. If my current page is for example: server/website/Pages/Page.aspx, so I get document.location.href = server/website/Pages/Login.aspx which actually is located under: server/website/Login.aspxEldwin
Ah, well in that case you can't use Javascript only. You need to use a server-sided language like C# or PHP to calculate the real URL.Hypersensitive
C
1

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.

Chorion answered 3/1, 2013 at 8:55 Comment(0)
S
1

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

https://mcmap.net/q/1417606/-get-root-website-url-in-javascript-for-redirect

will return http://stackoverflow.com

Synthetic answered 3/4, 2017 at 1:21 Comment(0)
C
0

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

Condottiere answered 5/1, 2015 at 16:43 Comment(0)
C
0

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"

Chessboard answered 7/9, 2016 at 9:37 Comment(0)
B
0
function homepage_of(url) { 
    var arr = url.replace('//','@@').split("/");
    return arr[0].replace('@@','//');
}
Blanc answered 26/10, 2019 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.