Does putting scripts on the bottom of a web page speed up page load?

前端 未结 8 853
别跟我提以往
别跟我提以往 2020-12-03 01:30

Yahoo best practices states that putting JavaScript files on bottom might make your pages load faster. What is the experience with this? What are the side effects, if any? <

8条回答
  •  日久生厌
    2020-12-03 01:53

    Putting them at the bottom is a close equivalent to using the "defer" attribute (even more info here). This is similar to how a browser cannot continue with page layout unless IMG tags have width and height information -- if the included javascript generates content, then the browser can't continue with layout until it knows what is there, and how big everything is.

    So long as your javascript doesn't need to run before the onload event happens, you should be able to either place the script tags at the end, or use the defer attribute.

提交回复
热议问题