Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if the JQuery CDN is down and coping with it

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 :)

like image 769
Karan Avatar asked Oct 20 '11 15:10

Karan


4 Answers

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>
like image 87
swatkins Avatar answered Nov 19 '22 09:11

swatkins


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>
like image 37
Manuel van Rijn Avatar answered Nov 19 '22 10:11

Manuel van Rijn


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.

like image 5
Joe Avatar answered Nov 19 '22 11:11

Joe


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>
like image 2
Declan Cook Avatar answered Nov 19 '22 09:11

Declan Cook