Page 1 of 1

defer="defer"

Posted: Mon Oct 30, 2017 2:37 pm
by Vegan
I was wondering for all the JaveScript in wordpress I was thinking why not load them all asynchronously so that Chome etc can grab all the parts at once and perk up rendering the site faster?

The idea is the have: <script defer="defer" src="myscript.js> which will load in parallel.

Now PHP can so some work such as: return str_replace( ' src', ' defer="defer" src', $tag );

Now some scripts already have defer, so I was wondering, a duplicate should be OK but to be smart, should I not check first?

Not sure the best spot yet to install my idea, thoughts? Found this code on https://wordpress.stackexchange.com/que ... 8335#38335

Code: Select all

add_filter( 'script_loader_tag', function ( $tag, $handle ) {

    if ( 'contact-form-7' !== $handle )
        return $tag;

    return str_replace( ' src', ' defer="defer" src', $tag );
}, 10, 2 );