Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 616
Inbal Avatar asked Jan 03 '13 08:01

Inbal


People also ask

How to get base URL JavaScript?

In Chrome Dev Tools, you can simply enter window. location in your console and it will return all of the available properties.

How do I find the root URL?

You can determine your server's root URL by browsing to a page on your website and observing the address in the location/address entry field of the browser. The root URL is the section between the colon-slash-slash (://) and the next slash (/), omitting any port number (:portno).


1 Answers

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

like image 163
Johan Hörnqvist Avatar answered Oct 10 '22 09:10

Johan Hörnqvist