views:

227

answers:

6

When I use Jquery or its plugin, should I just add

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; 
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"&gt;&lt;/script&gt;

Or should I download both files and upload both to my web server so my web server can fetch both files locally? Which is better and why?

+7  A: 

Better than that, let Google host them for you. See What is the AJAX Libraries API?. You can load them asynchronously or just reference the static files.

For example:

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

Whereas Google has offered this service so it's fine to use it, I'm not sure jQuery has so unless someone explicitly says "you can link stuff from my site" you should err on the side of caution and not (potentially) steal their bandwidth.

cletus
+1, Cletus is correct. Google's jquery files are hosted on a pretty crazy CDN that is routed among many locations to get the file to your users as fast as possible.
Simucal
Just to be specific: I believe the jQuery project would prefer that people not hotlink script files from their servers. If everyone did that, their bills would go astronomical in pretty short order.
Voyagerfan5761
A: 

There are valid arguments for both hosting your jQuery files locally or externally (usually via google).

The major argument against linking from an external server is that you're adding another point of failure to your website, but then again, google doesn't go down very often.

The argument in favor is a slight performance boost due to the fact that google uses a CDN for their files and that external files can be downloaded in parallel. Here's a great article that makes the case for this setup.

If you're not sure, try downloading FireBug and check to see what kind (if any) of a performance boost you get.

Chris Pebble
A: 

Another thought to consider (depending on the audience of your site) is your bandwidth cost. If you have to send file(s) to a huge number of clients, it can add up. Someone else (like Google) hosting that means that they're dealing with that, and not you.

reallyJim
A: 

Something different, if you have a lot of loose JS files (jQuery plugins?), then you could also consider minifying and merging them all into a single file on your host with a far-future expire time and gzip encoding. This saves lengthy (external) requests. Minifying and merging can easily be automated before deploy or during server startup.

BalusC
+3  A: 

I know it's a bit silly but, if you go for external host, just make sure you will never need to show your website anywhere that doesn't have a internet connection.

It's pretty unusual but hapenned to me once. I dad to show a new version of an already running site to a client in a presentation room with no internet.

Another case is an intranet site. If internet is down nobody will be able to connect to the intranet because it has a link to google .js or somewhere else.

:D

Túlio Caraciolo
A: 

Aside from an external dependency and yet another point of failure, per Chris' point, referencing files at a different domain means more DNS lookups. Merging files (where possible) and minifying also improves performance, as BalusC mentioned. Lots of good information can be found at http://developer.yahoo.com/performance/rules.html and there are a lot of very helpful tools available as well some of which follow:

ScottD