views:

266

answers:

6

I've done some searching on this but can't find any sort of best practice or recommendation -- and perhaps that's because there isn't any. I'm using all sorts of jQuery plugins on my site, in addition to TinyMCE and some other plugins, and my site's specific JS file. On top of that, I have three stylesheets ..... and there's more coming as the site grows!

Is there any sort of 'limit' or anything you should keep an eye out for including scripts in the tag? I know that each one is another HTTP request that needs to be completed, but if they are all minified and as small as can be, will there be issues?

    <link rel="stylesheet" type="text/css" href="http://mysite.com/assets/css/redmond.css" />
    <link rel="stylesheet" type="text/css" href="http://mysite.com/assets/css/style.css" />
    <link rel="stylesheet" type="text/css" href="http://mysite.com/assets/css/jqueryFileTree.css" />

    <!--[if gte IE 7]>
     <link rel="stylesheet" type="text/css" href="http://mysite.com/scripts/style.ie.css" />
    <![endif]-->

    <script type="text/javascript" src="http://mysite.com/assets/js/jquery.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://mysite.com/assets/js/jquery-ui.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://mysite.com/assets/js/jqueryFileTree.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://mysite.com/assets/js/jqminmax.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://mysite.com/assets/js/jquery.corner.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://mysite.com/assets/js/jquery.jeditable.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://mysite.com/assets/js/jquery.qtip.js"&gt;&lt;/script&gt;

    <script type="text/javascript" src="http://mysite.com/assets/plugins/tinymce/tiny_mce.js"&gt;&lt;/script&gt;

    <script type="text/javascript" src="http://mysite.com/assets/js/latitude.js"&gt;&lt;/script&gt;
+1  A: 

The more CSS and JS files you reference on a page the longer it'll take to load. It's much better to combine them into as few files as possible.

Eifion
Many browsers will open 2 concurrent connections to the server, effectivly paralellizing the downloading, there's tricks you can do in storing content on other domains/subdomains to trick the browser into setting up more than those 2 connections to download stuff. It's a balance between size and nr of files.
nos
+1  A: 

If it helps you feel better many other sites are "loaded" like that and js files are cached pretty much by all modern ( and even old ) browsers. One thing that might help would be linking your jquery file to one hosted on the googleapis site, because there could be a chance that some other site is linking to the same jquery file hosted there and it's already in their cache:

http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js

Your web server should already optimize files by automatically enabling gzip compression, but double check to make sure.

meder
It will still be very slow on "cold cache" loads e.g. first visit. And there is data from Yahoo that says a large %age of site hits are cold cache.
Frozenskys
+6  A: 

Not good!

I would reccomend combining these files into one js and one css using a script before deploying to live. There are many reasons why this is not good, see the link below for more info:

http://developer.yahoo.com/performance/rules.html

Edit: To answer further No, there are no limits on the number of files, but most browsers can only have 2 connections (for a site) to a web server at any one time and thus will load your js 2 files at a time. The YSlow plugin for firefox will show you a graph of how your files are being downloaded.

If you are using ASP.NET this codeproject article might help, if you are on linux then this might help.

Edit: A simple batch file on windows (to plug into your build tools) would be

@echo off
copy file1.js + file2.js + file3.js merged.js /b
Frozenskys
How do I combine the JS files? Just copy paste all the code into one giant thing then minify it?
Nathan Loding
C/P after minification should work too.
Sii
Between this and the YUICompress library, I think I should be OK. Thanks for all the input!
Nathan Loding
I believe the limit is generally 4 not 2, but it's browser dependent anyway. Also, the situation is worse - the browser will only download concurrently for images etc. When it encounters a script it has to download that script on its own in case something needs executing.
Draemon
A: 

I don't know if there's exists a good answer for this kind of question, but if you're interested in optimization, there's a whole number of them done by Yahoo!: http://developer.yahoo.com/performance/rules.html.

Personally, i tend to ignore most of those rules, simply because the kind of things i'm building aren't all that big.

JHollanti
+4  A: 

Browsers have a limit to the number of concurrent requests to a domain, so they can slow down the loading of other items in your page. For example, IE8 has a limit of 6 concurrent connections, and IE7 has a limit of 2.

I'd recommend combining the files you host locally, referencing others externally (e.g. on Google), and potentially moving others to the bottom of the page so they don't delay load.

You can combine scripts with a lot of online tools, like jsCompress. It's got an "Upload Files" tab, so you can just upload all your js files and it'll compress and minify.

There are also server-side utilities that will do that for you. You don't say what server-side technology you're using, but for instance in the PHP world you can use Minify.

Jon Galloway
A: 

Google Page Speed is your friend. Seems to be basically YSlow on steroids.

Sii