Here is an approach that works specifically with the Microsoft Optimization Framework as you provided in the OP
angularjsRoute.CdnFallbackExpression = @"
function() {
try {
window.angular.module('ngRoute');
} catch(e) {
return false;
}
return true;
})(";
This expression is not valid javascript itself, but the MS Optimization Framework uses this and eventually produces the following output to the page. Now we have a valid self-executing javascript function that returns true or false based on whether the angular module loads.
<script>(
function() {
try {
window.angular.module('ngRoute');
}
catch(e) {
return false;
}
return true;
})()||document.write('<script src="/bundles/scripts/angularjs-route"><\/script>');</script>