untitledgraphic
8/29/2019 - 5:26 AM

A script to defer loading of non-essential render blocking scripts until after the page fully has loaded complete with CSS.

A script to defer loading of non-essential render blocking scripts until after the page fully has loaded complete with CSS.

<script>
	document.addEventListener("DOMContentLoaded", theDomHasLoaded, false);

	function theDomHasLoaded(e) {
		[
			// The scripts to be loaded on the page
            // DO NOT include scripts that need to be loaded before the dom has fully loaded eg moderniizr.js
			'path/to/script.js',
			'path/to/another-script.js'
		].forEach(function(src) {
			var script = document.createElement('script');
			script.src = src;
			script.async = false;
			document.head.appendChild(script);
		});
	}
</script>