• How do you defer parsing of javascript with Primer?
    I tried several methods:
    1. I’ve tried a few plugins
    2. I tried adding this to header.php:

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

    3. I tried adding this to functions.php and replacing with the correct URL.

    	<script type="text/javascript">
    		function parseJSAtOnload() {
    		var element = document.createElement("script");
    		element.src = "somejavascriptfiles.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>
    

    I’m using Stout, a Primer child theme. But even with just Primer I get this issue. Though I did not try the plugin method with just Primer. The issue goes away when I change to the Twenty Twenty theme.

    Has anyone successfully defered parsing of javascript with Primer?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @dashbarkhuss

    What is it you are trying to do? This doesn’t seem specific to Primer, but more of a general WordPress development question.

    You should hook into wp_enqueue_scripts and output the scripts there. I’m also not entirely sure what the clean_url filter is that you’ve got there.

    Thread Starter dashbarkhuss

    (@dashbarkhuss)

    @eherman24, I’m trying to defer the javascript from rendering before the critical content of the page. https://technumero.com/defer-parsing-of-javascript/

    The reason I’m saying the issue is related to primer- all the techniques that I’ve found (listed in OP) seem to work on other themes but not Primer. So I’m wondering what is keeping these techniques from working on my Primer theme.

    I will look into your suggestions. I’m not so familiar with the wordpress framework outside of the CMS so your suggestions are new to me. But I have experience with programming (though little php) so I think I can learn about it and figure it out.

    • This reply was modified 4 years, 6 months ago by dashbarkhuss.
    • This reply was modified 4 years, 6 months ago by dashbarkhuss.
    Thread Starter dashbarkhuss

    (@dashbarkhuss)

    Looks like it’s related to the wp fastest cache plugin not the theme.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Defer Javascript Parsing’ is closed to new replies.