window.location.href going a folder back?

Viewed 4438

So I have a html document that is located at localhost/r/index.php, but when I do window.location.href="localhost" it sends me to locahost/r/localhost which I don't want. I want my script to send me to localhost. Is there a way to do this?

5 Answers

You use / to denote the root directory of your webserver:

window.location.href="/";

Or, you can use a full url:

window.location.href="http://localhost";

window.location.href="localhost" => window.location.href="/"

The path of the link is relative to the current page. You should use a path pointing to the root (i.e. localhost), so you should replace your code with window.location.href="/".

If you're already on localhost, then use / to go to the root, but in general, if you want to go to a particular domain, you need to include a scheme or //:

window.location.href = 'http://localhost';

// or

// uses the same scheme as whatever is currently displayed
window.location.href = '//localhost';   

You need to set the location to a full url such as http://localhost

for instance:

window.location = "http://localhost";

You can verify this in the console.

Related