Modernizr 文件应该放在 head 中吗?
对 Modernizr JavaScript 文件的引用是否应该位于页面的头部?我总是尝试将所有脚本放在页面底部,并希望保留这一点。如果它需要在头脑中,为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
对 Modernizr JavaScript 文件的引用是否应该位于页面的头部?我总是尝试将所有脚本放在页面底部,并希望保留这一点。如果它需要在头脑中,为什么?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
如果您希望Modernizr尽快下载并执行,以防止FOUC,将其放入
来自他们的 安装指南:
If you want Modernizr to download and execute as soon as possible to prevent a FOUC, put it in the
<head>
From their installation guide:
我认为不会:您放置在
中的每个脚本都会阻止渲染和进一步的脚本执行。 Modernizr 所做的唯一必须发生在
中的事情是集成的html5shiv,它将 HTML5 标记支持融入到 Internet Explorer 8 及更早版本中。
我昨天测试了这个并发现这是相当重要的– 在我工作的网站上,该网站已经得到了相当好的优化,在 IE9 中将单个脚本添加到头部延迟了大约 100 毫秒的加载时间,这甚至没有从 shiv 中受益!
由于我大约 90% 的流量来自原生支持 HTML5 的浏览器,并且我没有核心 CSS(需要 Modernizr 类在初始渲染上正确显示),因此我使用这种方法,将 html5shiv 放入条件注释中,在没有集成垫片的情况下加载现代化:
I would argue no: every script you place in the
<head>
will block rendering and further script execution. The only thing Modernizr does which must happen in the<head>
is the integrated html5shiv, which hacks HTML5 tag support into Internet Explorer 8 and earlier.I was testing this yesterday and found this to be fairly significant – on the site I work on, which is already fairly well optimized, adding that single script to the head delayed my load-time by ~100ms in IE9, which doesn't even benefit from the shiv!
Since around 90% of my traffic comes from browsers which natively support HTML5 and I don't have core CSS which requires the modernizr classes to display correctly on the initial render, I'm using this approach which places the html5shiv into a conditional comment and loads modernizr without the integrated shim:
保罗·爱尔兰现在建议对于> 75% 的网站将 Modernizr 放在
head
中没有任何好处。https://github.com/h5bp/html5-boilerplate/issues/1605
Paul Irish is now suggesting that for > 75% of sites, there is no benefit to placing Modernizr in the
head
.https://github.com/h5bp/html5-boilerplate/issues/1605
以稍微不同的方式使用 IE 条件怎么样?
大家觉得这个解决方案怎么样:
在
标签内:
在
标签结束之前:
这会导致在 Modernizr 中,对于 IE8 及以下版本,它会在 header 中加载,对于任何其他浏览器,它将在 body 之前加载。
欢迎在评论中公开讨论利弊。
How about using IE conditionals in a slightly different way?
What does everyone think of this solution:
Within the
<head></head>
tags:Before the end of the
</body>
tag:This would result in Modernizr loading in the head for IE8 and below, and it will load before the body for any other browsers.
Open discussion on pros and cons welcome in the comments.