How to load Google Jquery library Async or Defer?
-
I am using this code to disable wordpress default jquery and load from Google library to speed up website and reduce latency for this Big jquery File.
But Here i want to make it defer or async loading, So How we can do this ?
Here is code which i am using in my function.php file:-
//Making jQuery to load from Google Library
function replace_jquery() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, ‘https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js’, false, ‘1.11.3’);
wp_enqueue_script(‘jquery’);
}
}
add_action(‘init’, ‘replace_jquery’);I have tried this in function.php to make it async:-
add_filter( ‘script_loader_tag’, function ( $tag, $handle ) {
if ( ‘jquery/1.11.3/jquery.min.js’ !== $handle )
return $tag;return str_replace( ‘ src’, ‘ async=”async” src’, $tag );
}, 10, 2 );
- The topic ‘How to load Google Jquery library Async or Defer?’ is closed to new replies.