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.
In Chrome Dev Tools, you can simply enter window. location in your console and it will return all of the available properties.
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).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With