窗口位置链接href返回一个文件夹吗?

5

我有一个html文档,位于localhost/r/index.php。但是,当我执行window.location.href="localhost"时,它会将我发送到locahost/r/localhost,这不是我想要的结果。我希望我的脚本把我发送到localhost。有没有办法做到这一点?


4
只需要使用斜杠 / 即可。 - stelioslogothetis
5个回答

7
你需要使用 / 来表示你的web服务器的根目录:
window.location.href="/";

或者,您也可以使用完整的网址:

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

4

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

意思是将浏览器的当前URL从"localhost"更改为"/"。

2

如果你已经在本地主机上,那么使用/即可跳转到根目录,但通常来说,如果想要访问特定的域名,则需要包含协议或//

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

// or

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

2

链接的路径是相对于当前页面的。你应该使用指向根目录的路径(例如localhost),因此你应该将你的代码替换为window.location.href="/"


2

您需要将location设置为完整的URL,例如:http://localhost

例如:

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

你可以在控制台中验证这一点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接