Forked from jonathanstark/adding-js-programmatically.html
Created
June 28, 2019 02:19
-
-
Save zaagan/0afa3fd30f5b1c9525f78580c086b5b0 to your computer and use it in GitHub Desktop.
Snippet of javascript code that will append external script files programmatically, and in order. Intended for responsive web sites where maximum progressive enhancement is desired. Don't want to make needless http requests or load external javascript on devices that can't (or shouldn't) execute javascript. Any questions/comments/suggestions gre…
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
<html> | |
<head></head> | |
<body> | |
<!-- All your kewl content goes here --> | |
<!-- Append javascript programatically so we don't make needless http requests --> | |
<script> | |
(function(doc){ | |
var appendScripts = function(srcs) { | |
if (src = srcs.shift()) { | |
var scriptTag = doc.createElement('SCRIPT'); | |
scriptTag.src = src; | |
scriptTag.onload = function(){appendScripts(srcs)}; | |
doc.body.appendChild(scriptTag); | |
} | |
} | |
var goodBrowser = function() { | |
// Return true if browser passes all feature tests | |
return true; | |
} | |
if(goodBrowser()) { | |
appendScripts([ | |
'./js/script1.js', | |
'./js/script2.js' | |
]); | |
} | |
})(document); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment