Title: Defer Javascript Parsing
Last modified: May 15, 2020

---

# Defer Javascript Parsing

 *  [dashbarkhuss](https://wordpress.org/support/users/dashbarkhuss/)
 * (@dashbarkhuss)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/defer-javascript-parsing/)
 * 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fdefer-javascript-parsing%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Evan Herman](https://wordpress.org/support/users/eherman24/)
 * (@eherman24)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/defer-javascript-parsing/#post-12841644)
 * Hi [@dashbarkhuss](https://wordpress.org/support/users/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](https://wordpress.org/support/users/dashbarkhuss/)
 * (@dashbarkhuss)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/defer-javascript-parsing/#post-12845476)
 * [@eherman24](https://wordpress.org/support/users/eherman24/), I’m trying to defer
   the javascript from rendering before the critical content of the page. [https://technumero.com/defer-parsing-of-javascript/](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 5 years, 10 months ago by [dashbarkhuss](https://wordpress.org/support/users/dashbarkhuss/).
    -  This reply was modified 5 years, 10 months ago by [dashbarkhuss](https://wordpress.org/support/users/dashbarkhuss/).
 *  Thread Starter [dashbarkhuss](https://wordpress.org/support/users/dashbarkhuss/)
 * (@dashbarkhuss)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/defer-javascript-parsing/#post-12855415)
 * 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.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/primer/1.8.10/screenshot.png)
 * Primer
 * [Support Threads](https://wordpress.org/support/theme/primer/)
 * [Active Topics](https://wordpress.org/support/theme/primer/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/primer/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/primer/reviews/)

## Tags

 * [gtmetrix](https://wordpress.org/support/topic-tag/gtmetrix/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [Parsing](https://wordpress.org/support/topic-tag/parsing/)

 * 3 replies
 * 2 participants
 * Last reply from: [dashbarkhuss](https://wordpress.org/support/users/dashbarkhuss/)
 * Last activity: [5 years, 10 months ago](https://wordpress.org/support/topic/defer-javascript-parsing/#post-12855415)
 * Status: not resolved