Michael Brunton-Spall     About     Archive     Feed

Javascript libraries and offline support

Published on 2009-10-26 19:49:17 +0000

A quick one here.  I develop most of the functionality to this website when I am offline on the train.  I wanted to use the jQuery library on my website, and the most performant way of doing so is to use Googles javascript mirror. (Yes I know about the privacy implications).  However that doesn't work offline, rendering my website into non-jquery mode and making it a bugger to implement jquery features.

After removing that call and checking it in a few times by accident I did something I've never seen anybody else do.

	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2/jquery.min.js" type="text/javascript"></script>	<script type="text/javascript">	if (window.jQuery == null) {	document.write('<scrip'+'t src="/static/js/jquery.js"></scr'+'ipt>');	}	</script>

 

This is absolutely no use to anybody browsing my website in it's deployed form, and I should put a wrapper around that to only use it in developer mode, but what it does is simple and perfect for me.

First it gets jquery from googles servers.

Then it checks to see if jquery is actually loaded.  If not it adds a script tag that loads jQuery from a relative URL.

 

That is all