8

I'm launching my own site tomorrow, and I'm expecting a couple hundred visits.

I wrote this script based on what PHP.net told me:

<?
$f = fsockopen("code.jquery.com", 80, $errno, $errstr, 30); 
if(!$f){
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>'; }
else {
echo '<script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>'; }
?>

Basically, if code.jquery.com's CDN is ever down (like it was about 20 minutes ago), then the Google API Library will kick in. I've tried it whilst jQuery was up, and it works, but just in case it goes down again, will this script actually work? (by switching over to Google's library) I can't really test it, I'd have to make code.jquery.com go offline. lol.

I'd do CSS replacements, but my entire site is based off of jQuery and Ajax, so I kind of really need it to function at all times. I'd host it on my own site, but my website isn't anywhere as fast as code.jquery.com or googleapis.com when they're running fine.

Thanks alot! Any responses greatly appreciated :)

Joe
  • 46,419
  • 33
  • 155
  • 245
Karan
  • 267
  • 3
  • 8
  • 2
    Why don't you link to googleapis.com exclusively? `https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js` should be there pretty much forever. – Blazemonger Oct 20 '11 at 15:23
  • 1
    Why prefer code.jquery.com over google? Seems like google would be your first choice. – BNL Oct 20 '11 at 15:23
  • H@BNL Haha, personal preference, I tend to remember the code.jquery.com URL, and I keep forgetting the Google URL for it :\ Thanks for your comment, never the less :) @mblase75 same reason ^^ :P although after reading your comments, I might as well had put Google's first in this case – Karan Oct 20 '11 at 15:37

5 Answers5

25

Why would you do this server side? it would make more sense for this to be done client side:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="http://code.jquery.com/jquery-1.6.4.min.js">\x3C/script>')</script>

This first tries to load the library from google. If it loads, then window.jQuery will be available. If not, then it tries to load from code.jquery.com. You can even put in another after those two to load a local version if neither load:

<script>window.jQuery || document.write('<script src="my/js/jquery-1.6.4.min.js">\x3C/script>')</script>
swatkins
  • 13,530
  • 4
  • 46
  • 78
  • +1 for the +1 realization :P As well as that Thanks for your answer! I didn't realize it was possible in JS! Thanks for your answer! :) – Karan Oct 20 '11 at 15:43
  • Hello, I am experiencing issues with this. code.jquery.com seems to be very slow right now and this script waits a very long time (>20s!) before it falls back to the local version. Is there a way to set a threshold time after which the "external" request is discarded? in my sense, if there is no answer after 3 seconds, cdns are not worth it anymore... – Vincent Pazeller Apr 06 '16 at 13:29
  • @Vincent Pazeller You can try using a setTimeout and check if the jQuery global is available. Have a look here: http://stackoverflow.com/a/2021325/844726 – swatkins Apr 06 '16 at 14:11
11

or you could do this with basic html:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
  • I don't understand this. Please can you explain? – Mob Oct 20 '11 at 15:26
  • `window.jQuery` will evaluate to true if jQuery has been successfully loaded, else write to the document to load it from the secondary source. – Mike Oct 20 '11 at 15:32
5

It won't do you much good. Putting a fetch into every request will slow things down a huge amount. Also, if you have upstream caching, the change won't be reflected anyway.

I would go for a totally javascript solution: try loading one and if it doesn't work, try the other.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • 5
    Not only that, but just because one's own server can/cannot reach another server, that doesn't tell you much about whether the client can reach the server. – Pointy Oct 20 '11 at 15:24
  • I didn't know that was possible in JavaScript, until about a minute ago! haha, I just realized from another person's answer that by testing jQuery right after the first fetch that works out well! Thanks for your answer! – Karan Oct 20 '11 at 15:39
2

I believe this would work fine but this adds a lot of overhead to every request made to your server.

Do it client side with JavaScript,

<script src=http://code.jquery.com/jquery-1.6.4.min.js"></script>
 <script>window.jQuery || document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js">\x3C/script>')</script>
Declan Cook
  • 6,066
  • 2
  • 35
  • 52
0

You can test (on a local/test server) by simply putting an entry for code.jquery.com in your hosts file (/etc/hosts on linux, windows\system32\drivers\etc\hosts on windows) pointing at, for example, a local ip which is not used on your network. The system will temporarily see that as DNS for code-jquery.com and will not get a response from there.

Adam
  • 6,539
  • 3
  • 39
  • 65