What can I do to decrease load times of HTML pages?

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.


Minify your HTML source, CSS source, and JS Source. Gzip if possible.

http://code.google.com/p/htmlcompressor/

For JavaScript try: http://code.google.com/closure/compiler/


To start off, you should use a tool such as YSlow (Firefox and Chrome extensions available) or Googe Page Speed Online. There are others are out there I'm sure. These tools will grade your sites performance in different areas and provide tips on how you can improve them.

After using these tools for a while you'll start to change the way you build your pages and factor in these extra steps.

You could also look at async script loaders for your JavaScript files. A popular one is head.js. A search on Google should give you tones more articles on more in depth techniques such as this.

链接地址: http://www.djcxy.com/p/41516.html

上一篇: 哪些浏览器支持<script async =“async”/>?

下一篇: 我能做些什么来减少HTML页面的加载时间?