Defer loading javascript / Eliminate render-blocking JavaScript


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)


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";
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

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.



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

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