Thursday, 12 September 2013

Javascript has grown and CDN failures - Oh My!

For a long time I considered javascript to be a fairly childish language (and rightfully so since my days of working with Javascript goes back to the original navigator days) and I had actually lost track of the language  for over a decade!  I have been recently looking at the language as it seems to have become this new programming language for the real time web and frankly surprised by the number of concepts, frameworks and capabilities that it has evolved into over the years.

Coming to more practical matters, I had a steep learning curve  - learnt how they managed to not pollute the global namespace with global variables, how they have classes (sort of...) and how they have objects (sort off...) and in the middle of all this feeling that the world of javascript is just one big hashtable.

But duty calls and I have been attempting to use some of the popular frameworks out there - socket.io, json, jQuery and so on.  That is when I discovered these wonderful CDNs from Google, Microsoft and Cloudflare that will provide hosting for all these popular frameworks.  Now there is an entire school of thought on why CDNs are bad, I as always have an opinion of my own that does not quite agree with the general population so I decided to use the CDNs and still wanted to fall-back to my own servers in case the CDN is down  (yes that is an extremely unlikely event but call me whatever - remember we didn't pay for it).

I found an elegant solution in the Javascript language itself and I thought you may want to use.

Here is how I load JQuery from CDN or fallback to the origin servers when I have to...
 <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' charset='utf-8' type='text/javascript'></script> 
< script type="text/javascript">
// <![CDATA[
            window.jQuery || document.write('<script src="http://i.myorigin.com/fqstatic/js/jquery/jquery.min.js">\x3C/script>')
// ]]> 
 
 
Hope this helps 

No comments:

Post a comment