简体   繁体   English

之前是放置脚本标记 </body> 标记相当于jQuery的document.ready方法

[英]Is placing script tag before </body> tag equivalent to jQuery's document.ready method

If we call a javascript method myMethod() in a script tag which is before closing body, is it equivalent to calling myMethod() inside jQuery's document.ready function ? 如果我们在关闭body之前的脚本标记中调用javascript方法myMethod() ,它是否相当于在jQuery的document.ready函数中调用myMethod() If not, Why ? 如果没有,为什么?

From here : 这里

Under the hood: $(document).ready() As you would expect from John Resig, jQuery's method for determining when the DOM is ready uses an assortment of optimizations. 引擎盖下:$(document).ready()正如您对John Resig所期望的那样,jQuery确定DOM何时就绪的方法使用了各种各样的优化。 For example, if a browser supports the DOMContentLoaded event (as many non-IE browsers do), then it will fire on that event. 例如,如果浏览器支持DOMContentLoaded事件(就像许多非IE浏览器那样),那么它将触发该事件。 However, IE can't safely fire until the document's readyState reaches “complete”, which is typically later. 但是,在文档的readyState达到“完成”之前,IE无法安全地触发,这通常是后来的。 If none of those optimizations are available, window.onload will trigger the event. 如果这些优化都不可用,window.onload将触发该事件。

These events are independent of a location within the HTML tag, as other event still are going on even at the time of rendering </body> . 这些事件独立于HTML标记内的位置,因为即使在呈现</body>时其他事件仍在进行中。

不,它不一样,你将<script>标签放在结束</body> <script>标签之前,以避免阻止在旧版浏览器AFAIK上呈现html,但你不能保证DOM已经“准备好”

Not exactly. 不完全是。 $(document).ready(); reacts on the so called DOMContentLoaded event which is fired right after the DOM has been loaded and the browser is aware of all the elements on the page (not the content itself). 对所谓的DOMContentLoaded事件做出反应,该事件在加载DOM之后立即触发,并且浏览器知道页面上的所有元素(而不是内容本身)。

The main reason why code is usually put inside these blocks is not that much related to preventing blocking of parallel loading but to ensure that the elements which are to be manipulated during page load are actually loaded and present in the DOM tree. 代码通常放在这些块中的主要原因与防止并行加载的阻塞并没有多大关系,而是确保在页面加载期间要操作的元素实际加载并存在于DOM树中。 Not much sense in manipulating elements which the browser is not aware of right? 操纵浏览器不知道的元素没什么意义吗?

Putting JavaScript content (or any other content for that matter) at the bottom of the page is actually more closely related to the onload event which is fired after the page has finished loading, including the content itself. 将JavaScript内容(或任何其他内容)放在页面底部实际上与页面加载完成后触发的onload事件(包括内容本身)更密切相关。 Either way its almost certain that content inside $(document).ready() blocks will be executed before the one at the bottom of the page however if you load external libraries on which the code inside the ready() block relies you can't put those at the bottom of the page. 无论哪种方式,几乎可以确定$(document).ready()块内的内容将在页面底部的内容之前执行,但是如果你加载了ready()块中的代码所依赖的外部库你就不能把它们放在页面底部。

In general if you have code that isn't dependent on external libraries and a successful loading of DOM you can safely put it at the bottom of the page. 通常,如果您的代码不依赖于外部库并且成功加载DOM,则可以安全地将其放在页面底部。 If you however have stuff that needs to be executed right after the DOM has been loaded you most definitely want that code in the $(document).ready() block, but do have in mind that you can place that block wherever you want, even in the middle of the page (which can be a nice trick sometimes). 如果你有东西需要在加载DOM后立即执行,你绝对想要$(document).ready()块中的代码,但是请记住你可以把那个块放在任何你想要的地方,甚至在页面的中间(有时可能是一个很好的技巧)。

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

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