我可以如何减少HTML页面的加载时间? [英] What can I do to decrease load times of HTML pages?

查看:146
本文介绍了我可以如何减少HTML页面的加载时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意 :这是一个社区Wiki帖子



最好的,我能做些什么来使我的HTML页面的加载更有效率?

解决方案



CSS组织


$ b $ 在处理页面效果时,有几个重要的方法可以加快页面加载速度。 b

尝试最小化内联CSS样式,并将常用的CSS规则保存在外部样式表中。



缩小



p>由于您的CSS和Javascript包括必须从您的服务器下载到客户端,小的总是更好。雅虎有一个伟大的工具,名为 YUI Compressor ,可用于减小CSS和JavaScript的大小。诸如JQuery的热门库也将具有其库的缩减版和开发版。



图片压缩



您可以想考虑压缩你的图像。对于JPG文件,尝试设置大约80%的压缩,并查看结果的外观。你可以玩水平,直到你得到一个体面的结果。对于PNG文件,您可能需要查看一些 PNG压缩工具



CSS Sprites



保存HTTP请求的一个有趣的策略是使用CSS Sprites。基本理论是不是下载多个图像,你只需下载一个大图像,其中包含所有的图像。这意味着,不是连续请求图像文件,浏览器只需要一个请求。教程 CSS Sprites:它们是什么,为什么它们很酷,以及如何使用它们有一些很好的信息该过程包括如何从现有的多图像布局进行转换。



资源排序



要订购你的CSS和Javascript,你希望你的CSS先来。原因是渲染线程具有渲染页面所需的所有样式信息。如果Javascript包括第一个,Javascript引擎必须解析它,然后继续下一组资源。这意味着渲染线程不能完全显示页面,因为它没有所需的所有样式。下面是一个例子:

 < link rel = stylesheettype =text / csshref =/ css / global.css/> 
< link rel =stylesheettype =text / csshref =/ css / forms.css/>
< script type =text / javascriptsrc =/ js / formvalidation.js>< / script>



跟踪/关联脚本位置



网站使用跟踪和/或附属脚本。如果远程主机有问题,并且脚本包含在< head> 标记中,则浏览器必须等待下载发生才能继续。虽然这样的事情是很好的,他们不应该减慢用户体验。建议将此类脚本移动到页面底部,紧靠< / body> 标签之前:

 <! -  HTML Here  - > 
< script type =text / javascript>
var gaJsHost =((https:== document.location.protocol)?https:// ssl。:http:// www。
document.write(unescape(%3Cscript src ='+ gaJsHost +google-analytics.com/ga.js'type ='text / javascript'%3E%3C / script%3E));
< / script>
< / body>



缺少资产



缺少CSS和JavaScript文件意味着浏览器必须不必要地与服务器通信以抓取不存在的文件。根据服务器的位置和缺少多少文件,这可能会导致加载速度较慢的网页。


Note: This is meant to be a community wiki post

To try and make user experience the best possible, what can I do to make the loading of my HTML pages more efficient?

解决方案

When dealing with performance of pages, there are a few important methods to keeping your page load times quick.

CSS Organization

Try to minimize inline CSS styles and keep commonly used CSS rules together in external stylesheets. This helps keep reusable styles for later, and the lack of style attributes makes your HTML page download faster.

Minification

Since your CSS and Javascript includes have to be downloaded from your server to the client, smaller is always better. Yahoo has a great tool called YUI Compressor which can be used to reduce the size of your CSS and JavaScript includes. Popular libraries such as JQuery will also have both minified and development versions of their libraries available. Just remember to keep a copy of the non-minified version for debugging purposes!

Image Compression

You may want to consider compressing your images. For JPG files, try setting around 80% compression, and seeing how the result looks. You can play around with the levels until you get a decent result. For PNG files, you may want to look at some of the PNG compression tools available.

CSS Sprites

An interesting tactic in saving HTTP requests is the usage of CSS Sprites. The basic theory is instead of downloading multiple images, you simply download one large image with all of your images contained within it. This means instead of making continuous requests for image files, the browser just needs to make a single request. The tutorial CSS Sprites: What They Are, Why They’re Cool, and How To Use Them has some good information on the process, including how to convert from an existing multi-image layout.

Resource Ordering

When it comes to ordering your CSS and Javascript, you want your CSS to come first. The reason is that the rendering thread has all the style information it needs to rendering the page. If the Javascript includes come first, the Javascript engine has to parse it all before continuing on to the next set of resources. This means the rendering thread can't completely show the page, since it doesn't have all the styles it needs. Here is an example:

<link rel="stylesheet" type="text/css" href="/css/global.css" />
<link rel="stylesheet" type="text/css" href="/css/forms.css" />
<script type="text/javascript" src="/js/formvalidation.js"></script>

Tracking / Affiliate Script Locations

Many sites utilize tracking and/or affiliate scripts. If there is an issue with the remote host, and the scripts are included in the <head> tag, the browser has to wait for the downloads to occur before moving along. While such things are nice to have, they shouldn't slow down the user experience. It is recommended to move such scripts towards the bottom of the page, just before the </body> tag:

<!-- HTML Here -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
</body>

Missing Assets

Missing CSS and javascript files means the browser has to needlessly communicate with the server to grab files that don't exist. Depending on where the server is and how many files are missing, that could add up to slower page loads.

这篇关于我可以如何减少HTML页面的加载时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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