最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.location.href going a folder back? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Oct 1, 2017 at 15:59 steliosbl 8,9214 gold badges31 silver badges57 bronze badges asked Oct 1, 2017 at 15:49 Elias KonradElias Konrad 1081 silver badge9 bronze badges 1
  • 4 Just / should work – steliosbl Commented Oct 1, 2017 at 15:51
Add a comment  | 

5 Answers 5

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论