Defer loading javascript / Eliminate render-blocking JavaScript

Hi,

Performed a search but not seen anything to resolve this issue.

Have just built my site using Wordpress and have checked it on Pagespeed Insights. It scores 85/100 Mobile and 95/100 Desktop. The one fix it suggests is to Eliminate render-blocking JavaScript (1)

/wp-includes/js/jquery/jquery.js?ver=1.12.4

I found what looks an interesting article here : Defer loading javascript

In the article, the author says to insert this code into your site:

<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>

Not sure exactly where to place this code in a Wordpress site. I assumed the header.php file but this hasn’t worked.

I just wondered if anyone has used this method and found it effective. It’s been a while since I have been involved building sites so this is all new for me.

Thanks for any pointers or advice.

Regards

WEL

If you have control over the html there’s no need for tricks like this these days. You can put the script just before the closing body tag.

<script src="defer.js" defer></script>
</body>
</html>
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.