Local fallback jQuery (with a twist of Joomla)
It’s been a while since the last post, but here were go again anyway! Today there was a small problem with OpenDNS whereby anyone using their service would find that Google’s content delivery network was blocked as a phishing site.
Oh well you say, but Google’s CDN hosts jQuery and have a look at how many sites use this hosted version….
Needless to say I found myself in the situation this morning where a lot of my websites appeared to have failed, along with a large portion of the web. Once DNS settings were changed however things got back to normal. This got me thinking though, that if the CDN does actually go down for real my sites will need a redundant fallback.
This is to say that a local version of jQuery can be stored on the server for the site to use if it detects that the requested CDN version can not be found
With a slight Joomla tailoring, to do this you need to:
Download a version of jQuery. (from jquery.com or similar)
Create a directory on your site called js. I placed mine in /templates/js
Put the downloaded jQuery file in this directory.
Create a second file in this directory called loader.js
Within this file, put the following content:
This will check if jQuery has been loaded and if not add a script tag to the current page to load the fallback jQuery file.
$document = JFactory::getDocument(); $document->addScript('http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'); $document->addScript(JURI::root() . 'templates/js/loader.js'); //Other includes can go here