Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

javascript - 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 the user experience the best possible, what can I do to make the loading of my HTML pages more efficient?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...