我有一个html文档,位于localhost/r/index.php
。但是,当我执行window.location.href="localhost"
时,它会将我发送到locahost/r/localhost
,这不是我想要的结果。我希望我的脚本把我发送到localhost
。有没有办法做到这一点?
我有一个html文档,位于localhost/r/index.php
。但是,当我执行window.location.href="localhost"
时,它会将我发送到locahost/r/localhost
,这不是我想要的结果。我希望我的脚本把我发送到localhost
。有没有办法做到这一点?
/
来表示你的web服务器的根目录:window.location.href="/";
或者,您也可以使用完整的网址:
window.location.href="http://localhost";
window.location.href="localhost"
=>
window.location.href="/"
如果你已经在本地主机上,那么使用/
即可跳转到根目录,但通常来说,如果想要访问特定的域名,则需要包含协议或//
:
window.location.href = 'http://localhost';
// or
// uses the same scheme as whatever is currently displayed
window.location.href = '//localhost';
链接的路径是相对于当前页面的。你应该使用指向根目录的路径(例如localhost
),因此你应该将你的代码替换为window.location.href="/"
。
您需要将location
设置为完整的URL,例如:http://localhost
例如:
window.location = "http://localhost";
/
即可。 - stelioslogothetis