should Modernizr file be placed in head?

只谈情不闲聊 提交于 2019-12-03 01:48:44

问题


Should the reference to the Modernizr JavaScript file be in the head of the page? I always try and place all scripts on the bottom of the page and would like to preserve this. And if it needs to be in the head, why?


回答1:


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:

Drop the script tags in the <head> of your HTML. For best performance, you should have them follow after your stylesheet references. The reason we recommend placing Modernizr in the head is two-fold: the HTML5 Shiv (that enables HTML5 elements in IE) must execute before the <body>, and if you’re using any of the CSS classes that Modernizr adds, you’ll want to prevent a FOUC.




回答2:


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:

<html>
    <head>
        …
        <!--[if lt IE 9]>
            <script src="html5shiv.min.js"></script>
        <![endif]-->
    </head>
    <body>
        …
        <script src="modernizr.custom.min.js"></script>
    </body>
</html>



回答3:


Paul Irish is now suggesting that for > 75% of sites, there is no benefit to placing Modernizr in the head.

Move Modernizr to the bottom

It has more potential to introduce unexpected situations, however it's much better for the user to just have no scripts up in the head at all.

I bet >75% of sites dont need it in the head. I'd rather change this default and educate the 25% than watch as we slow down all these sites.

https://github.com/h5bp/html5-boilerplate/issues/1605




回答4:


How about using IE conditionals in a slightly different way? What does everyone think of this solution:

Within the <head></head> tags:

<!--[if lt IE 9 ]>
<script src="/path/to/modernizr.js"></script>
<![endif]-->
</head>

Before the end of the </body> tag:

<!--[if gt IE 8]><!-->
<script src="/path/to/modernizr.js"></script>
<!--<![endif]-->
</body>

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.



来源:https://stackoverflow.com/questions/6272702/should-modernizr-file-be-placed-in-head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!