Created
June 1, 2012 16:25
-
-
Save jacksonfdam/2853345 to your computer and use it in GitHub Desktop.
The Best Way to Load jQuery Library (with fallback)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Load jQuery Library | |
<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline --> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script>window.jQuery || document.write('<script src="js/ext/jquery-1.7.2.min.js"><\/script>')</script> | |
<!-- Grab Google CDN's jQuery UI, with a protocol relative URL; fall back to local if offline --> | |
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> | |
<script>window.jQuery.ui || document.write('<script src="js/ext/jquery-ui.min.js"><\/script>')</script> | |
//Load jQuery library using plain JavaScript | |
<script> | |
//Load jQuery library using plain JavaScript | |
(function(){ | |
var newscript = document.createElement('script'); | |
newscript.type = 'text/javascript'; | |
newscript.async = true; | |
newscript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'; | |
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript); | |
})(); | |
(function () { | |
function loadScript(url, callback) { | |
var script = document.createElement("script") | |
script.type = "text/javascript"; | |
if (script.readyState) { //IE | |
script.onreadystatechange = function () { | |
if (script.readyState == "loaded" || script.readyState == "complete") { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
}; | |
} else { //Others | |
script.onload = function () { | |
callback(); | |
}; | |
} | |
script.src = url; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} | |
loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function () { | |
//jQuery loaded | |
console.log('jquery loaded'); | |
}); | |
})(); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment