Is $(document).ready necessary if I put all my JavaScript at the bottom of the page? [duplicate]

Communitympen提出了一个问题:Is $(document).ready necessary if I put all my JavaScript at the bottom of the page? [duplicate],或许与您遇到的问题类似。

回答者CommunityThatGuy给出了该问题的处理方式:

This SO answer says NO:

stackoveflow question

$(document).ready is for assurance full DOM is available at the time the function is called. Any functions and events not depending on the DOM don't need to be put into the ready event.

Also - to improve page rendering speed - load javascript files dynamically in non-blocking fashion: http://berklee.github.com/nbl/ or https://github.com/rgrove/lazyload/

This technique works somewhat like this:

 var script = document.createElement("script");
 script.type = "text/javascript";
 script.src = "file1.js";
 document.getElementsByTagName("head")[0].appendChild(script);

This new element loads the source file file1.js. The file begins downloading as soon as the element is added to the page. The important thing about this technique is that the file is downloaded and executed without blocking other page processes, regardless of where the download is initiated. You can even place this code in the header of a document without affecting the rest of the page (aside from the one HTTP connection that is used to download the file).

this book: "High Performance JavaScript" by Nickolas Zakas has a lot of interesting information about JavaScript performace optimization.

希望本文对你有帮助,欢迎支持JavaScript中文网