Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1402357

    LTWWordpress
    Participant

    Hello!

    Our website has a whopping 0% grade on GTMetrix for “Defer parsing of Javascript,” and that “2.9MiB of JavaScript is parsed during initial page load.”

    According to https://varvy.com/pagespeed/defer-loading-javascript.html, I should add this code right before the </body> tag:

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

    My only issue is that I can’t figure out how to get to the HTML right before </body> to then be able to add this coding! If you guys can help me out, I’d be very appreciative.

    http://www.LTW1.com

    #1402364

    LTWWordpress
    Participant
    This reply has been marked as private.
    #1402793

    Rad
    Moderator

    Hi there,

    Thanks for writing in.

    Please add this code to your child theme’s functions.php

    add_action('wp_footer', 'before_closing_body', 9999999);
    function before_closing_body() { ?>
    <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>
    <?php }

    Hope this helps.