I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.
I'm not a big fan of document.write
but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.
It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
In place of the document.write
I might put something like:
var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
if (xmlHttpReq.readyState == 4) {
(window.execScript || window.eval).call(window, xmlHttpReq.responseText);
}
}
xmlHttpReq.send(null);
This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)
After some Google Searching I've found a few places that do the almost same as your framework in less code.
<scrip.t<script src="/path/to/cdn/framwork.js"></scrip.t>script>
<scrip.t><script> window.framework || document.write("<scrip.t"<script src=\"/local/path/framework.js\"></scrip.t>"script>");</scrip.t>script>
It is shorter but requires one per script file.