我的很多页面都有大量的 jquery。
我正在考虑将它们放入一个带有 $(document).ready(function() {
以及里面的所有内容。
这是一个好主意还是坏主意?
如果要执行更多代码(即使与页面无关),每个页面总体上是否会变慢?我想当 dom 准备好时,外部脚本中的每一行代码都会被执行......?还是我的理解有误?
最佳答案
will each page be slower overall if there is more code to execute even if its not relvamnt to the page?
外部脚本文件可能会产生一些加载开销,但如果您在多于一页的任意数量的页面上使用该脚本,则外部脚本文件是一个好主意;它将被缓存并且是即时的。
i imagine each line of code in the external script gets executed when the dom is ready..? or is my understanding wrong?
是的。如果您将代码包装在函数中作为 $(document).ready()
的参数,它将在 DOMContentLoaded
上执行。
关于javascript - 将 jquery 和 javascript 放入外部文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166094/