Forked from jonathanstark/adding-js-programmatically.html
Created
January 3, 2019 15:37
-
-
Save SHEHANhasintha/879a98fd822c61c32461fc28a72a2740 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 hidden or 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