JavaScript是否应该进入<body>以获得性能,而不是<head>?

Sta*_*ein 4 html javascript

IBM的网站谈论快速Web开发这里提到一个有用的骨架HTML.在模板中,脚本包含在body中而不是head中.这是一个好习惯吗?将任何库放在头部不是更好吗?

<html>
  <head>
    <title>Template</title>
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap-responsive.min.css" rel="stylesheet">
  </head>
  <body>
    <!-- The main HTML will go here -->
    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

VS

<html>
  <head>
    <title>Template</title>
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap-responsive.min.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </head>
  <body>
    <!-- The main HTML will go here -->
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

m59*_*m59 10

现在,标准是在关闭body标记之前包含脚本标记,以便脚本加载不会阻止页面加载的其余部分.

  <script src="myScript.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)

有了这个,用户将不必等待在页面上看到某些内容,然后添加javascript功能.

但是,有越来越多的网站和"网络应用程序"javascript/ajax很重,可能需要在页面上显示任何内容之前加载脚本.这种情况不太常见,但是这种情况可以将脚本包含在任一位置,因为如果javascript负责创建/加载内容,则视觉结果将是相同的.

要验证:以下是Google的建议:https://developers.google.com/apps-script/guides/html/best-practices#load_javascript_last

还要考虑从CDN加载库,以便您可以利用浏览器缓存.