最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I place Jquery files on my web server or just refer to them via jquery.com in my PHP files, which is better?

programmeradmin3浏览0评论

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

<script type="text/javascript" src=".js"></script> 
<script type="text/javascript" src=".validate.js"></script>

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?

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

<script type="text/javascript" src="http://code.jquery./jquery-latest.js"></script> 
<script type="text/javascript" src="http://dev.jquery./view/trunk/plugins/validate/jquery.validate.js"></script>

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?

Share Improve this question edited Jan 26, 2012 at 7:24 oers 18.7k13 gold badges68 silver badges76 bronze badges asked Dec 3, 2009 at 1:54 StevenSteven 25.4k45 gold badges112 silver badges137 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 8

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./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.

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

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.

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.

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.

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./performance/rules.html and there are a lot of very helpful tools available as well some of which follow:

  • http://developer.yahoo./yslow/
  • http://www.webpagetest/
  • http://code.google./speed/page-speed/
  • http://getfirebug./

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论