【发布时间】:2015-09-16 05:24:39
【问题描述】:
我是 JS 和一般编程的新手,希望有人可以帮助我。
我目前正在构建一个网站,其中每个页面都有其单独的 HTML / PHP 文件。
jQuery 和我的全局/通用 JS 函数通过单独的包含文件“footer.php”包含在所有这些页面的页脚中。
到目前为止,一切都按预期工作。
现在我有一些页面将使用特定的 jQuery 函数,所以我只想在加载了这样的页面时才加载相应的 JS。
为此,我将相应的代码保存在单独的 JS 文件中(例如 nameOfExternalJsFile.js)并将其中的所有内容包装在以下内容中:
$(document).ready(function(){
// ...
});
然后我对相应的 PHP 页面进行了以下更新(示例):
<head>
<?php
require_once("includes/header.php");
?>
<!-- here I want to include the specific jQuery functions -->
<script src="includes/js/nameOfExternalJsFile.js"></script>
</head>
<!-- ... -->
<!-- here I include the main JS functions -->
<?php require_once("includes/footer.php"); ?>
我对此有两个担忧:
- 我不确定这是否是包含此类文件的正确方法,因为 我需要准备好文件。
- 我将我的主要 JS 包含在页脚中,因为我被告知这会改进 性能,但我可以在标题中包含 jQuery 函数吗?
有人可以告诉我这是否正确,或者我是否应该在这里更改任何内容?
非常感谢您对此提供的任何帮助。
【问题讨论】:
标签: javascript jquery html include document-ready