There are many reasons why you want to defer parsing of Javascript in WordPress. One of the most obvious reasons is speed. Normally, Javascript is being place in the between <head> </head>. When we are loading a website, what it does is to load from top to bottom. Therefore, if you  have a lot of Javascript code or a long string of code there, it will take very long before the website’s content will appear, because it needs to wait until all Javascript codes are finished. In order to speed up the site, we can defer parsing of Javascript, so the website doesn’t have to wait for Javascript codes to finish. There are basically two ways to do that, beginner’s way and more advanced way.

Let’s begin with the advanced way: coding. Basically, all you need to do is to copy the following code into the function.php of your theme:


function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return "$url.' async onload='myinit()";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

Or

function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return "$url' defer ";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

and save it. However, before to place any code into your function.php, make sure you have ftp or cPanel login information ready, and backup  your function.php file first, because codes in function.php is very sensitive in a way, any missing codes will trigger errors on your site, and you won’t be able to login or reverse if you do not have ftp or cPanel login information ready.

Related:  How to Make Sticky Posts in WordPress

After that is done, you should notice increase in speed of your website. Always double check all features on your site, make sure they all works as they should.

Now, let’s move on to the easier one. The second way to defer parsing of Javascript in WordPress is by installing a plugin call “BJ Lazy Load”. You can simply go to plugins -> add New, and search for it. After found it, you can install and activate, and you are done. This plugin might have conflict with some of theme’s functionality, so double check after activated.

Of course, there is another plugin called “WP Defer Loading”, which is just like what it’s named, defer loading javscript, just what Google wants.

The above are three most common ways to defer Parsing of Javascript in WordPress, if you know a better way,  please let us know.