Related Posts Plugin for WordPress, Blogger...
Custom Search

Thursday, August 25, 2011

Contain CSS and JavaScript in External Files




Many of these performance rules deal with how external components are managed. However, before these considerations arise we should ask a more basic question: Should JavaScript and CSS be contained in external files, or inlined in the page itself?

Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests.

The key factor, then, is the frequency with which external JavaScript and CSS components are cached relative to the number of HTML documents requested. This factor, although difficult to quantify, can be gauged using various metrics. If users on our site have multiple page views per session and many of our pages re-use the same scripts and stylesheets, there is a greater potential benefit from cached external files.

Many web sites fall in the middle of these metrics. For these sites, the best solution generally is to deploy the JavaScript and CSS as external files. The only exception where inlining is preferable is with home pages. Home pages that have few (perhaps only one) page view per session may find that inlining JavaScript and CSS results in faster end-user response times.

For front pages that are typically the first of many page views, there are techniques that leverage the reduction of HTTP requests that inlining provides, as well as the caching benefits achieved through using external files. One such technique is to inline JavaScript and CSS in the front page, but dynamically download the external files after the page has finished loading. Subsequent pages would reference the external files that should already be in the browser's cache.


Do you Like this Post ?

Get Free Email Updates Daily!

Follow us!


Free Sitemap Generator

2 Comment:

Nice explanation! I have stated this before on one of my tips on web developing/designing. It is true that having scripts and styles being external files makes the loading of the site faster. :) I also stated some examples there to on how to add external files to blogging sites like blogger.

Again nice post! pretty informative!

Aj Banda,,thank you...i hope it will be helpful for us.

Post a Comment

Do you like this post? Give your comment...

Subscribe to Posts (Atom)

Add to Google Reader or Homepage

Subscribe in Bloglines


Subscribe via Email



Get Tweets!

 
Return to Top