将 JavaScript 放在 HTML 文件中的什么位置? [英] Where to place JavaScript in an HTML file?

查看:23
本文介绍了将 JavaScript 放在 HTML 文件中的什么位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个相当大的 JavaScript 文件,压缩到大约 100kb 左右.我所说的文件是指通过 <script src="..."> 链接的外部文件,而不是粘贴到 HTML 本身中.

Say I have a fairly hefty JavaScript file, packed down to roughly 100kb or so. By file I mean it’s an external file that would be linked in via <script src="...">, not pasted into the HTML itself.

将它放在 HTML 中的最佳位置是什么?

Where’s the best place to put this in the HTML?

<html>
<head>
    <!-- here? -->
    <link rel="stylesheet" href="stylez.css" type="text/css" />
    <!-- here? -->
</head>
<body>
    <!-- here? -->
    <p>All the page content ...</p>
    <!-- or here? -->
</body>
</html>

每个选项之间会有任何功能差异吗?

Will there be any functional difference between each of the options?

推荐答案

The Yahoo!卓越性能团队建议将脚本放在页面底部,因为浏览器的方式下载组件.

The Yahoo! Exceptional Performance team recommend placing scripts at the bottom of your page because of the way browsers download components.

当然,Levi 的评论就在您需要它之前,不要很快"确实是正确的答案,即视情况而定".

Of course Levi's comment "just before you need it and no sooner" is really the correct answer, i.e. "it depends".

这篇关于将 JavaScript 放在 HTML 文件中的什么位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆