简体   繁体   English

jQuery:如果在页面底部使用外部JS,为什么要使用document.ready?

[英]jQuery: Why use document.ready if external JS at bottom of page?

I am including all of my JS as external files that are loaded at the very bottom of the page. 我将所有JS作为外部文件包含在页面的最底部。 Within these files, I have several methods defined like so, which I call from the ready event: 在这些文件中,我有几个像这样定义的方法,我从ready事件中调用它:

var SomeNamepsace = {};

SomeNamepsace.firstMethod = function () {
    // do something
};

SomeNamepsace.secondMethod = function () {
    // do something else
};

$(document).ready(function () {
    SomeNamepsace.firstMethod();
    SomeNamepsace.secondMethod();
});

However, when I remove the ready function and call the methods straight-up, everything works just the same, but executes significantly faster—almost a whole second faster on a pretty basic file! 但是,当我删除ready函数并直接调用方法时,一切都运行相同,但执行速度明显更快 - 在一个非常基本的文件上几乎快一秒! Since the document should be loaded at this point (as all the markup comes before the script tags), is there any good reason to still be using the ready event? 由于此时应该加载文档(因为所有标记都出现在脚本标记之前),还有什么理由仍然使用ready事件吗?

Great question. 好问题。

There is some confusion around the whole "put scripts at the bottom of your page" advice and what problem(s) it attempts to solve. 围绕整个“在页面底部放置脚本”的建议以及它试图解决的问题存在一些混淆。 For this question I am not going to talk about whether putting scripts at the bottom of the page affects performance/loadtimes or not. 对于这个问题,我不打算讨论是否在页面底部放置脚本会影响性能/加载时间。 I am only going to talk about whether you need $(document).ready if you also put scripts at the bottom of the page . 我只想谈谈你是否需要$(document).ready 如果你也把脚本放在页面底部

I'm assuming you are referencing the DOM in those functions you are immediately invoking in your scripts (anything as simple as document or document.getElementById ). 我假设您正在引用您在脚本中立即调用的那些函数中的DOM(任何像documentdocument.getElementById这样简单的函数)。 I'm also assuming you are asking only about these [DOM-referencing] files. 我还假设你只询问这些[DOM-referencing]文件。 IOW, library scripts or scripts that your DOM-referencing code requires (like jQuery) need to be placed earlier in the page. IOW,您的DOM引用代码所需的库脚本或脚本(如jQuery)需要在页面的前面放置。

To answer your question : if you include your DOM-referencing scripts at the bottom of the page, No, you do not need $(document).ready . 回答你的问题 :如果你在页面底部包含你的DOM引用脚本,不,你不需要$(document).ready

Explanation : without the aid of "onload" -related implementations like $(document).ready the rule of thumb is: any code that interacts with DOM elements within the page should to be placed/included further down the page than the elements it references. 解释没有 "onload"相关的实现,比如$(document).ready ,经验法则是:与页面中DOM元素交互的任何代码都应该放在/包含在页面的下方,而不是它引用的元素。 The easiest thing to do is to place that code before the closing </body> . 最简单的方法是在结束</body>之前放置该代码。 See here and here . 看到这里这里 It also works around IE's dreaded "Operation aborted" error . 它也适用于IE的可怕“操作中止”错误

Having said that, this by no means invalidates the use of $(document).ready . 话虽如此,这绝不会使$(document).ready的使用无效。 Referencing an object before it has been loaded is [one of] the most common mistakes made when beginning in DOM JavaScript (witnessed it too many times to count). 在加载对象之前引用一个对象是在DOM JavaScript中开始时最常见的错误之一(见证了它太多次计算)。 It is jQuery's solution to the problem, and it doesn't require you to have to think about where this script will be included relative to the DOM elements it references. 它是jQuery的问题解决方案,它不需要你考虑这个脚本相对于它引用的DOM元素的位置。 This is a huge win for developers. 这对开发者来说是一个巨大的胜利。 It's just one less thing they have to think about. 这只是他们不得不考虑的一件事。

Also, it's often difficult or impractical to move all DOM-referencing scripts to the bottom of the page (for example any script that issues document.write calls have to stay put). 此外,将所有DOM引用脚本移动到页面底部通常很困难或不切实际(例如,任何发出document.write调用的脚本都必须保持不变)。 Other times, you are using a framework that renders some template or creates pieces of dynamic javascript, within which references functions that need to be included before the js. 其他时候,您正在使用一个框架来渲染一些模板或创建动态javascript片段,其中引用需要在js 之前包含的函数。

Finally, it used to be "best practice" to jam all DOM-referencing code into window.onload , however it has been eclipsed by $(document).ready implementations for well document reasons . 最后,它曾经是将所有DOM引用代码插入window.onload “最佳实践”,但是由于文档原因 ,它已被$(document).ready实现黯然失色。

All this adds up to $(document).ready as a far superior, practical and general solution to the problem of referencing DOM elements too early. 所有这些加起来就是$(document).ready作为一个非常优越,实用和通用的解决方案来解决过早引用DOM元素的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM