Support » Fixing WordPress » How to load Google Jquery library Async or Defer?

  • Resolved yashveer

    (@yashveer)


    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’, ‘http://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 );

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think this line is not correct.
    if ( 'jquery/1.11.3/jquery.min.js' !== $handle )

    It should be like so:
    if ( 'jquery' !== $handle )

    Thanks!!

    Its worked for me.

    Even wordpress.stackexchange is failed in this question !!

    Now its not useful for me

    Reason:- Too much time for loading JavaScript as well as not working on Some browser..

    Now Returned back to default code !!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to load Google Jquery library Async or Defer?’ is closed to new replies.