4
votes

I am working on a website and working on its Page Speed Insights and have a

Eliminate render-blocking JavaScript and CSS in above-the-fold content

So basically what happen is I have two external javascript import on top of the page which is:

<script src="/remotestaff_2015/js/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="/remotestaff_2015/js/bootstrap.min.js" type="text/javascript"></script>

This website has lot of javascripts but some are at the bottom of the page after the <body> tag. So my problem is those two on the top. To fix the render block I already searched on google and found out that I can use the defer or async attribute. My problem is that there are some modules on the website that has inline javascripts like:

<script type="text/javascript">

$("#menu-toggle").click(function(e) {

    e.preventDefault();

    $("#wrapper").toggleClass("toggled");

    if ($("#wrapper").hasClass("toggled")) {

        $(".color-fade").css("opacity", ".5");

    } else {

        $(".color-fade").css("opacity", "1");

    }

});

So if I put defer on my jquery file I'll have an error on my inline javascripts because it will load without the jquery loaded yet. Is there a way to fix this inline scripts or somehow make them run after the deferred jquery or other js files has been loaded? I don't want to put those inline scripts in other file.

3

3 Answers

1
votes

According to MDN:

This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

Therefore let your inline script wait for the document and the deferred scripts to be loaded. The DOMContentLoaded event will fire when that has happened:

    <script>
        document.addEventListener('DOMContentLoaded', function() {
          // Inline scripts go here
        });
    </script>

Notice that the $(document).ready(function() is not required any more, because that would wait for the same event (DOMContentLoaded).

0
votes

you can load your js dynamically in

$(window).bind("load", function() { // code here });

for dynamically loading scripts you ca use code

var script = document.createElement("script")
script.type = "text/javascript";

if (script.readyState){  //IE
    script.onreadystatechange = function(){
        if (script.readyState == "loaded" ||
                script.readyState == "complete"){
            script.onreadystatechange = null;
            callback();
        }
    };
} else {  //Others
    script.onload = function(){
        callback();
    };
}

script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);

Please let me know if it helped you.

-1
votes

I would do this to make everything clean. Load jQuery file without defer and define menu toggling event in HTML head.

<script src="/remotestaff_2015/js/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function() {
        // option 1
        /* $('#menu-toggle').click(function() {
            // your code
        }); */
    });

    // option 2
    $(document).on('click', '#menu-toggle', function() {
        // your code
    });
</script>

Now you can render the element anywhere in HTML body or even from AJAX callback.