简体   繁体   English

渲染阻止的Javascript和CSS

[英]Render-blocking Javascript and CSS

It seems to me that anytime there is a <script src="name1.js"> or a <link href="name2.css"> statement in the <head> , these two files block rendering of the markup. 在我看来,任何时候有一个<script src="name1.js"><link href="name2.css">在声明中<head>这两个文件阻止标记的渲染。

FWIW, I have tried adding "async" to the <script> tag and it totally messes up some of my jQuery plug-ins FWIW,我尝试将<async <script>添加到<script>标记中,这完全弄乱了我的一些jQuery插件

Given that, I really do not understand the phrase "Render-blocking Javascript and CSS". 鉴于此,我真的不理解短语“ Render-blocking Javascript and CSS”。

Thanks! 谢谢!

They block rendering of markup because the browser parses the HTML file from top down. 它们阻止了标记的呈现,因为浏览器从上到下解析HTML文件。

You can avoid this by placing the script tag before the closing body : 您可以通过将script标记放在结束body之前来避免这种情况:

  <script src="whatever.js"></script>
</body>

You're out of luck with link elements, unless you just use inline style declarations in the head (probably not a good idea). 除非您仅在head使用内联style声明(否则可能不是一个好主意),否则您对link元素并不满意。

You can also use the async attribute in the script tag, or you can use one of any numerous async JavaScript libraries. 您也可以在script标签中使用async属性,也可以使用众多异步JavaScript库之一。

您也可以先尝试加载关键的CSS,然后再加载主/缩小的CSS文件,以避免CSS文件上的渲染阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM