I intend to call a js, which will not use anything when loading the page. Therefore, I need to call this js only when the entire page has been loaded, so as not to delay anything or load the screen for the user.
<html>
<body>
<script type="text/javascript" defer=defer src="function.js"></script>
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "function.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>
</body>
</html>
My question is: in the above ways, which one would be more efficient? I need to decrease the loading time of my page and with that I want to "postpone" everything that is not necessary.