我不能将 javascript 文件放在 html public 文件夹之外吗?

Can't I put my javascript files outside html public folder?

我正在关注 this tutorial 创建一个安全的登录系统。我的文件夹逻辑如下:

/var/www/html/mysite/ # html folder is public (here goes index.php, etc)
/var/www/includes/    # includes is hidden (login details, etc)
/var/www/js/          # js is also hidden, as stated on the tutorial

当我说"as stated on the tutorial"时,我指的是这段话:

Store your copy of this file in a directory called "js", off the root directory of the application.

我理解对了吗? js 文件夹应该在 html 内,而不是 www 吗?有什么不同?在我的代码中,我有这样的行:

include_once '../../includes/db_connect.php';
include_once '../../includes/functions.php';

<script type="text/JavaScript" src="../../js/sha512.js"></script> 
<script type="text/JavaScript" src="../../js/forms.js"></script> 

虽然 php 包括工作不错,但 javascript 却不行。 Firefox 检查器正在抱怨:

GET http://localhost/js/sha512.js         [HTTP/1.1 404 Not Found 0ms]

为什么考虑到我的 js 文件夹在 html 文件夹中,而不是我告诉它的地方?

我会尝试将 JS 放入 html 文件夹并像这样链接到它。 <script type="text/JavaScript" src="js/forms.js"></script>

如果 www 文件夹是根目录(即用于 http://your-url/ 的文件夹),那么您可以执行以下操作:

<script src="/js/sha512.js"></script> 
<script src="/js/forms.js"></script> 

前导 / 表示 "start at the root of the website"。

如果 html 文件夹是您的根目录,您需要将 js 文件夹移动到 html 文件夹中 - 您仍然可以使用上面显示的脚本标签.

JavaScript浏览器请求文件,一般public禁止访问您网站根目录以外的文件夹。您的 PHP 文件已加载到服务器上,这意味着您可以看到一般 public 无法访问的文件。