Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set window.location to a specific path (without a host)?

I am using the window location method to redirect a webpage to another after a set amount of time.

The url needs to change from www.myurl.com/home to www.myurl.com/other. The problem is that I do not know what the final URLs will be so I cannot use absolute links, they have to be a path only. This is what I have so far:

 window.location.pathname = "mobility.html"
like image 661
jimbouton Avatar asked Apr 11 '12 15:04

jimbouton


People also ask

How do I change Windows location host?

window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page. window.location.protocol returns the web protocol used (http: or https:) window.location.assign() loads a new document.

How do I find my Windows location URL?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

What does setting window location href do?

The location. href property sets or returns the entire URL of the current page.


2 Answers

You can just prepend a / to your URL to make them relative to the domain root (without having to hardcode the domain name). Like this:

window.location = "/mobility.html"
like image 195
bfavaretto Avatar answered Sep 26 '22 00:09

bfavaretto


window.location.assign("/path") also works.

like image 37
Shaun Luttin Avatar answered Sep 24 '22 00:09

Shaun Luttin