[英]window.location.href going a folder back?
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. 所以我有一个位于localhost/r/index.php
的html文档,但是当我执行window.location.href="localhost"
它会将我发送到locahost/r/localhost
,这是我不想要的。 I want my script to send me to localhost
. 我希望我的脚本将我发送到localhost
。 Is there a way to do this? 有没有办法做到这一点?
window.location.href="localhost"
=> window.location.href="/"
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";
The path of the link is relative to the current page. 链接的路径相对于当前页面。 You should use a path pointing to the root (ie localhost
), so you should replace your code with window.location.href="/"
. 您应该使用指向根的路径(即localhost
),因此您应该使用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 //
: 如果您已经在localhost
,那么使用/
转到root,但一般情况下,如果要转到特定域,则需要包含方案或//
:
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
您需要将location
设置为完整URL,例如http://localhost
for instance: 例如:
window.location = "http://localhost";
You can verify this in the console. 您可以在控制台中验证这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.