为什么标准把css和js文件放在页面的顶部和底部(分别)? [英] Why is the standard to put the css and js file at the top and bottom of the page (respectively)?

查看:439
本文介绍了为什么标准把css和js文件放在页面的顶部和底部(分别)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道为什么在标准化网站时使用标头和页面顶部/底部的css和js来提高性能。

Just curious why there is a performance enhancement when you standardize a site with the css and js in the headers and at the top/bottom of the page.

推荐答案

这不完全正确。简单地说:

That's not entirely correct. Said simply:


  • 样式声明应尽可能靠近顶部,因为浏览器在加载之前不会呈现您的页面CSS(避免闪烁的未定型内容)

  • Style declarations should be as close to the top as possible, since browsers won't render your page before loading the CSS (to avoid a flash of unstyled content)

脚本标记应尽可能靠近底部,因为它们阻止浏览器在标记后解析在加载和完成之前(因为脚本可以使用document.write更改文档)

Script tags should be as close to the bottom as possible, since they block browsers from parsing after the tag before it is loaded and complete (because the script may change the document with document.write)

对前端性能感兴趣,我强烈推荐阅读Steve Souders的高性能网站:前端工程师的基本知识

If you're interested in frontend performance, I highly recommend reading High Performance Web Sites: Essential Knowledge for Front-End Engineers by Steve Souders.

这篇关于为什么标准把css和js文件放在页面的顶部和底部(分别)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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