是否有关于应如何加载 javascript 的一揽子规则。我看到有人说它现在应该放在页面的末尾。
想法?
最佳答案
将它放在文档末尾的想法是确保在尝试引用文档中的元素之前已经下载了文档的全部内容。如果先加载 JavaScript,则代码可能会在文档未准备好时查找元素。
jQuery 使用 ready
解决了这个问题功能:
Binds a function to be executed whenever the DOM is ready to be traversed and manipulated.
This is probably the most important function included in the event module, as it can greatly improve the response times of your web applications.
还有一些性能方面的考虑建议在页面底部包含 JavaScript 文件 - 这是因为下载 JavaScript 文件可能会阻止所有其他 HTTP 请求(对于图像、CSS、等)直到完成。
关于javascript - javascript 代码应该始终加载在 html 文档的头部吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1766474/