It’s really frustrating when you google to find something and have to scroll through a blog of affiliate link garbage to get the answer you are looking for. So if you are trying to optimize your website and GTmetrix is telling you to “Defer parsing of JavaScript” here is a code you can try adding to your functions.php file that could help. Yes, there are plugins that exist that will load JavaScript deferred or will Async JavaScript, but I personally try to avoid additional plugins whenever possible.
Naturally, backup your website before you start altering code and run a website speed test with Pingdom or GTmetrix before and after to see if it helped. If you are using wordpress, it’s best to use a child theme with its own functions.php file so your updates will remain in place after theme updates and you don’t break your theme or website.
function defer_parsing_of_js( $url ) {
if ( is_user_logged_in() ) return $url; //don’t break WP Admin
if ( FALSE === strpos( $url, ‘.js’ ) ) return $url;
if ( strpos( $url, ‘jquery.js’ ) ) return $url;
return str_replace( ‘ src’, ‘ defer src’, $url );
}
add_filter( ‘script_loader_tag’, ‘defer_parsing_of_js’, 10 );
When you defer parsing of JavaScript, your website is taking unnecessary JavaScript and loading this JavaScript deferred so all the necessary code loads first. This can help speed up your website and improve your score with GTmetrix, Pingdom, and of course, Google, which is ultimately the reason you are doing this in the first place. Google’s M.O. is to improve the user internet experience, so improving your website functionality can help you improve your website & mobile SEO ranking with Google and other search engines.
Another method you can try is by adding the code below to your HTML just before your </body> tag which should be easy to find at the bottom of your HTML file. You will need to change the name of “defer.js” to the name of your external js file. If your js file is not in the same file as your HTML, then you will need to make sure the path to your js file is correct. You can find further explanation of the code below at Varvy, which is also a useful resource for website SEO and speed optimization.
<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>
If Google, GTmetrix, and/or Pingdom are giving you a laundry list of optimization for your WordPress website, do yourself a favor and just get WP Rocket, it’s one of the easiest and best caching plugins on the market and loaded with features for WordPress website optimization at a very low cost per year. If you read this, take a chance with W3 Total Cache, and break your website, then please remember this, “I told you so!”
Make sure you backup or have ftp access in case you have issues per usual. I personally no longer use a standard backup, I originally had a wordpress multisite and had a hell of a time figuring out how to export a website from a multisite. Here’s an affiliate free tip, I found a plugin you can use temporarily called, Prime Mover, that makes it super easy to export a website from a wordpress multisite or migrate a wordpress website in general. I have zero affiliation with them, I just am very grateful they exist. It’s an excellent plugin because you can export your wordpress website from a wordpress website or individual directory and have a complete backup. If something bad happens or you make a mistake, just install a fresh copy of WordPress, install the Prime Mover plugin, and bam, you’re back up and running like nothing happened. Best of all, IT IS FREE!!
A huge thank you to the Prime Mover developers! It’s quite a bit faster to use the premium feature and supports the developers if you are migrating multiple websites from a wordpress multisite but the free version still works beautifully and doubles as a backup. I highly recommend them and will let you search for them yourself in the wordpress plugins so you know I’m not profiting off this endorsement. I’m just a fan.