Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SHEHANhasintha/879a98fd822c61c32461fc28a72a2740 to your computer and use it in GitHub Desktop.
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…
<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