14

I have included a reference to Google's JQuery library in my markup:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

Is it safe to do this? How do I know Google won't change the address, or remove it altogether? I really can't afford to have my app break without warning.

What do other people do?

Urbycoz
  • 7,247
  • 20
  • 70
  • 108

5 Answers5

25

Have the best of both worlds. Use theirs for fast, possibly pre-cached, delivery and in case their server goes down (more likely than them moving it) fallback to your own:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
{
    document.write(unescape("%3Cscript src='/path/to/your/jquery' type='text/javascript'%3E%3C/script%3E"));
}
</script>

Taken from: Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail

Community
  • 1
  • 1
Mauvis Ledford
  • 40,827
  • 17
  • 81
  • 86
  • 1
    Really good way to do it, probably the best. However, that said, I don't plan on changing my code because I just can't imagine Google's CDN going down xP – Connor Smith Jun 17 '11 at 07:36
  • 2
    That's exactly what Paul Irish's HTML5 Boilerplate does. Also handy if, say, you're going to be working on something on your laptop while on a plane without internet access or somesuch (which happened to me a couple of weeks ago). – Scott Jun 17 '11 at 08:55
2

Yes, it's completely safe. It's also hosted on Google's CDN making it load faster, in most cases, than loading from your own server.

  • Not to mention a lot of people probably have google CDN resources cached, making it possibly even faster. – kinakuta Jun 17 '11 at 07:29
  • What about countries where Google domain name is blocked? – Urbycoz Jun 17 '11 at 07:33
  • @Urbycoz: that's what the fallback in Mauvis Ledford's answer is for, providing a local copy of jQuery just in case the CDN version is unavailable for whatever reason. – Scott Jun 17 '11 at 08:56
2

Absolutely, it is what you should do!

http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/

Connor Smith
  • 1,274
  • 7
  • 11
0

Google offers it for free. Google's servers are fast and above all you save your bandwidth.

Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
-1

It is 100% safe to use Google's hosted jQuery file. In fact, it is actually faster because browsers can download multiple files at once from different servers. Also, if the user has visited a website that uses Google's jQuery before, the script will already be in the cache, causing the page to load faster.

Ad@m

kirb
  • 2,033
  • 1
  • 18
  • 28