views:

70

answers:

4

What is the real benefit of using external css and js in place of placing code directly in ... and in terms of page loading speed?

if we are controlling whole site from one header.php/aspx file? Is use of external files makes page loading faster?

My question is only related to page loading speed.

+8  A: 

On a per-request basis (that is, looking at page load performance of JUST one page), speed-wise you actually take a small performance hit by separating the files. But when looking at performance, you get a performance boost when loading multiple pages that utilize the same JS or same CSS. In those cases, the content of the JS/CSS is only loaded once for all requests.

Although you didn't ask this as part of your question, it also helps code maintainability. If you make one change in your CSS and it gets loaded across multiple pages, if you embed the JS/CSS in the page then you have to make the same change across all of your pages.

Michael Bray
u mean external will be downloaded in user browser and if another page need same js then it will not be download again
metal-gear-solid
exactly. The browser knows that it is loading the same file, and so it only loads it once. that's browser caching.
Michael Bray
Yes, that's right - the browser will cache the JS and CSS files, although sometimes, they tend to be very aggressive about it, which leads to some tricks being done around that to ensure you can reliably push out updated CSS and scripts.
Rob
@Micheal Bray- yes but if we already using one header.php then whatever i will change in css or in js which is in <head> it will be also applied to whole site
metal-gear-solid
+3  A: 

External files can be cached, and have to be loaded only once even when referenced from multiple locations. This usually outweighs the performance hit caused by the one additional request when the resource is first loaded.

Pekka
+1  A: 

yes, the css and js files will be cached by the browser so they only get loaded once.

Paul Creasey
+3  A: 

i know you asked about page load speed and from that standpoint id say the greatest benefit id say cacheing is a big advantage but i wouldn't break it into multiple external files (because like first answer said it takes time to make a request) but you also get a benefit from a SEO standpoint... crawlers will only index to a certain point in a page and keeping js and css out of the top lets them see content higher in the page

i just found this article where a guy did the tests

http://articles.sitepoint.com/article/indexing-limits-where-bots-stop

Carter Cole
+1 Good point. Thanks
metal-gear-solid