How to defer parsing of JavaScript

fxkool

Regular Member
Joined
Feb 28, 2015
Messages
415
Reaction score
115
Hi All,

I have run my Wordpress through the GTmetrix tool and it came up with a few errors including deferring parsing of JavaScript. The grade score is very poor hovering around E(53) with the minimum of 71. I'd like to fix this up but unfortunately none of the tested html/php/plugin has worked out.

Functions PHP

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 )

Html (is it header or footer???)

<script type="text/javascript">
function parseJSAtOnload() {
var element = document.createElement("script");
element.src = "script_to_be_deferred.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", parseJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", parseJSAtOnload);
else window.onload = parseJSAtOnload;
</script>

Plugin

W3, Booster

Has anyone else come across the same issue in the recent past? Any help would be much appreciated.

Thanks,
S
 
Back
Top