Get url of root site in javascript to redirect

I want to redirect to the login page from every page on my website after the session ends. I am trying to set window.location to the login page:

var ParentUrl = encodeURIComponent(window.parent.location.href); document.location.href = "~/Login.appx?ReturnUrl=" + ParentUrl; 

but "~" seems unsupported. my login page is in my root folder.

for example: * HTTP: //server/website/*Login.aspx

How can I get this url in javascript?

Thank you very much,

Inbal.

+6
source share
7 answers

I would use window.location.origin. It will return the first part to you, and after that only Uri will encode the parent URL and it will be done!

 var parentUrl = encodeURIComponent(window.location.href), loginUrl = window.location.origin+"/Login.appx?ReturnUrl=" + parentUrl; window.location.href = loginUrl; 

A little tip for cross-browser functions is to use window.location. It reads / writes all compatible browsers. Although document.location is only read in some (i.e.).

+9
source

Why use ~? At first glance, I would say that removing it solves your problem. Like this.

  document.location.href = "/Login.appx?ReturnUrl=" + ParentUrl; 

[EDIT] replies to the first comment ...

I believe this can do the trick:

 function getLoginPage() { var urlParts = document.location.href.split("/"); return "/" + urlParts[2] + "/" + urlParts[3] + "/Login.aspx"; } document.location.href = getLoginPage() + "?ReturnUrl=" + ParentUrl; 
+2
source

The "/ website" part is usually server-side information. No way can JavaScript define this on its own.

So you have to pass this from the server to the client. You can also pass "http: // server / website" right away.

+1
source
 function getURL() { var arr = window.location.href.split("/"); delete arr[arr.length - 1]; return arr.join("/"); } 

You can use it as follows:

 document.location.href = getURL() + "Login.appx?ReturnUrl="; 

The difference between my function and the first answer is that "/" will be redirected to the server / page, and my code will be redirected (in the URL of your example) to the server / site / page.

+1
source

This function will return the URL of the root (base) 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

0
source

Usually I create a settings.js file in which I store a set of parameters for the application. In this case:

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

and then in a script, for example, I call

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

0
source

I modified PerKristian's answer so that it works in my situation,

 function getBaseUrl() { var re = new RegExp(/^.*\/\/[^\/]+/); return re.exec(window.location.href); } 

matches all to the first single /

eg

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

will return http://stackoverflow.com

0
source

All Articles