JavaScript window.location 函数在本地主机文件夹中不起作用

JavaScript window.location function not working in localhost folder

本文关键字:主机 文件夹 不起作用 window location 函数 JavaScript      更新时间:2023-09-26

在 xampp 的 htdocs 目录中,我创建了一个列表目录并在其中放置了一个简单的索引.html文件。

该文件包含

<!Doctype html>
<html>
   <head>
  <script>
        function Redirect() {
        alert("file:///D:/xampp/htdocs/");
           window.location="file:///D:/xampp/htdocs/";
        }
  </script>
</head>
    <body onload="Redirect();">
   </body>
</html>

当我导航到本地主机/列表/时,它显示一个空白页面,当我直接使用任何浏览器打开它时,它工作正常。

此网址"file:///D:/xampp/htdocs/"是否有任何限制。

我想查看带有 file://网址的文件结构。

你可以试试这个:

<script>
        function Redirect() {
        alert("../xampp/htdocs/");
           window.location="../xampp/htdocs/";
        }
  </script>

如果您已经在 xampp 子目录中,请使用相对 URL:

<!Doctype html>
<html>
   <head>
  <script>
        function Redirect() {
        alert("./htdocs/");
           window.location="./htdocs/";
        }
  </script>
</head>
    <body onload="Redirect();">
   </body>
</html>

实现此目的的更简单方法:

<!Doctype html>
<html>
   <head>
   <script>
        function Redirect() {
           alert("htdocs directory here");
           window.location="http://localhost/";
        }
   </script>
   </head>
   <body onload="Redirect();">
   </body>
</html>

由于HTDOCS将在内部调用本地主机