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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Inbal
  • 909
  • 2
  • 28
  • 46

8 Answers8

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

  • 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. `` – rococo Sep 12 '19 at 05:04
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;
Walter Brand
  • 679
  • 3
  • 9
  • I tried... but If my current page is for example: http://server/website/Pages/Page.aspx, so I get document.location.href = http://server/website/Pages/Login.aspx which actually is located under: http://server/website/Login.aspx – Inbal Jan 03 '13 at 08:58
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.

Dillen Meijboom
  • 963
  • 7
  • 13
  • 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 = http://server/website/Pages/Login.aspx which actually is located under: http://server/website/Login.aspx – Inbal Jan 03 '13 at 09:14
  • 1
    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. – Dillen Meijboom Jan 03 '13 at 09:43
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.

Jacco
  • 3,251
  • 1
  • 19
  • 29
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

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

will return http://stackoverflow.com

Adam Sparks
  • 497
  • 5
  • 12
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

Per Kristian
  • 785
  • 8
  • 10
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"

erionpc
  • 368
  • 3
  • 15
0
function homepage_of(url) { 
    var arr = url.replace('//','@@').split("/");
    return arr[0].replace('@@','//');
}
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42