Dear Magento users,
I have currently a question concerning the deferral of Javascript. I would like to use this code in order to defer javascript:
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src="defer.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>
I would like to paste this code just before the </body> tag. Therefore I would like to inquire what I have to do in order to realize it.
Thanks in advance!
I use my function before closed </body>
function dynamicLoadScript(url) { if (!url) return; var script = document.createElement("script"); script.type = "text/javascript"; script.src=url; script.async = true; // < defer (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script); }
and after it
dynamicLoadScript("defer.js");