window.location.href回一个文件夹?

时间:2017-10-01 15:49:08

标签: javascript html

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

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:4)

您使用/表示您的网络服务器的根目录:

window.location.href="/";

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

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

答案 2 :(得分:2)

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

答案 3 :(得分:2)

如果您已经在localhost上,请使用/转到根目录,但一般来说,如果您想要转到特定域,则需要包含一个方案或//

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

// or

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

答案 4 :(得分:1)

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

例如:

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

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