Skip to content

Instantly share code, notes, and snippets.

@jonathanstark
Last active September 29, 2021 06:48
Show Gist options
  • Save jonathanstark/4958789 to your computer and use it in GitHub Desktop.
Save jonathanstark/4958789 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…
<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>
@WebReflection
Copy link

@DataZombies
Copy link

This looks good. WR's code is interesting. The commas after the appendChild wouldn't work, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment