19

Right now I have the following links in my code:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>

etc ...

I'd like to make use of the google.com cached copies. I heard google is the best source but please correct me if I am wrong.

Anyway is it possible for me to code my application so it uses code from google if available and locally if not. FYI I am using Microsoft MVC3 and the Msoft cloud servers.

Thanks

jrummell
  • 42,637
  • 17
  • 112
  • 171
JudyJ
  • 597
  • 5
  • 7
  • 14
  • 2
    Extensive discussion here: http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-goo – James Montagne May 24 '11 at 18:38

2 Answers2

41

Sure, check out how they do it in HTML5 boilerplate.

If you take a look at the bottom of the index.html file within the GitHub repo, you'll see the following...

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/X.X.X/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="local/jquery-X.X.X.min.js">\x3C/script>')</script>

NB: In the code snippet above X.X.X should be replaced with the jQuery version number that you're using (e.g. 1.8.2).

How does it work?

  1. First, an attempt is made to grab the CDN version (Google's CDN url is used above, but of course you could link to any source you like).
  2. Immediately afterwards, we check for the jQuery global object.
  3. If jQuery does not exist, the obvious assumption is that we didn't manage to get the code from the CDN, so then we document.write a script tag to get a copy from a local source instead.
Community
  • 1
  • 1
isNaN1247
  • 17,793
  • 12
  • 71
  • 118
  • 1
    Depending on how you connection is "not online" it might take a long time though due to timeouts. – ThiefMaster May 28 '11 at 07:27
  • @TheifMaster Whilst I agree that performance wouldn't be great, this fallback is meant for when Googles CDN has failed to serve us. Not only would most people have a cached copy - those that didn't would be seeing a lot of ugly/broken sites online - whilst yours (with this fallback) would just be a bit slow – isNaN1247 May 28 '11 at 07:31
  • @beardtwizzle, it is more than a bit slow these days. I do a great deal of development offline, and it seems that the latest browsers (Firefox at least) wait much longer than it used to for these requests to resolve; meanwhile, your script is blocking. – harpo Jul 14 '13 at 00:21
  • Please see http://stackoverflow.com/questions/8231048/why-use-x3c-instead-of-when-generating-html-from-javascript if you wonder about \x3C :-) – MartyIX Jun 09 '15 at 10:35
  • Pity StackExchange does not use this fallback method. For any place which firewalls googleapis.com StackExchange pages will load but logon and feedback is disabled. – Underverse Jun 19 '16 at 09:09
8
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")">\x3C/script>')</script>
jessegavin
  • 74,067
  • 28
  • 136
  • 164