Here is a pure javascript solution that starts out by detecting if jQuery is available. If not, it tries the CDN version. If that's not available, it tries the local version. Handles 404 errors. I use this in a solution that doesn't know whether the website has included jQuery.
<script>
if (typeof jQuery === "undefined") {
loadjQuery("//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js", verifyJQueryCdnLoaded);
} else
main();
function verifyJQueryCdnLoaded() {
if (typeof jQuery === "undefined")
loadjQuery("script/jquery-1.6.1.js", main);
else
main();
}
function loadjQuery(url, callback) {
var script_tag = document.createElement('script');
script_tag.setAttribute("src", url)
script_tag.onload = callback; // Run callback once jQuery has loaded
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') callback();
}
script_tag.onerror = function() {
loadjQuery("script/jquery-1.6.1.js", main);
}
document.getElementsByTagName("head")[0].appendChild(script_tag);
}
function main() {
if (typeof jQuery === "undefined")
alert("jQuery not loaded.");
$(document).ready(function () {
// Rest of your code here...
});
}
</script>