ahe ahe - 17 days ago 6
HTTP Question

Javascript: redirect to different Subdomain

I've got a Landingpage: "https://www.example.com" and an Application: "https://app.example.com/something/" and on some user actions like logout i need to redirect the user back to the original landingpage.
my hard-coded solution does work:

window.location = "https://www.example.com"


but this is bad practice and annoying in other environments like my local development(http://localhost:8082/something - for the app, no local landingpage).
is there some good and easy solution to redirect to just the base-domain of my current site(example.com redirects to www.example.com by default)?

Answer

I would then use

var hostname = location.hostname;
window.location = "https://${hostname}";

source: Determine domain name in JavaScript?

also: http://www.w3schools.com/jsref/prop_loc_hostname.asp