...
Tutorials, Wordpress

How to Defer Parsing of Javascript in WordPress?

11 February 2019

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 . When we are loading a website.

you need to do is to copy the following code into the function.php of your theme:

Example 1:

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

Example 2:

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

Leave a Comments