Last active
September 29, 2021 06:48
-
-
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…
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> |
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
never mind, https://twitter.com/WebReflection/status/304041331121065984