0

Ok, i've tried all day long and do not know, how this can be. I have a website on which i load jquery like that:

function my_script_loader() {
if (!is_admin()) { // only load for front end
    //Clear jQuery if already registered
    wp_deregister_script('jquery');
    wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", array(), '1.11.1', '',true);
    wp_enqueue_script('jquery');
    } // End of if statement
} // End of Function
add_action('wp_enqueue_scripts', 'my_script_loader');

The expected result in the Dom is:

<script type="text/javascript" defer="defer" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">

Then i am doing the exact same thing on another website which is a subdomain:

function script_loader() {
if (!is_admin()) {
    //Clear jQuery if already registered
    wp_deregister_script('jquery');
    wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js", array() , '1.12.4', '',true);
    wp_enqueue_script('jquery');
    }
} // End of Function
add_action('wp_enqueue_scripts', 'script_loader');

The unexpected result in the Dom is:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js?ver=1.12.4">

I have disabled plugins and removed all scripts but the script is not deferred. How can that happen? Thanks for your interest. theo

  • AFAICT there is nothing in your code that adds the "defer". must be coming from another place – Mark Kaplun Jan 4 '17 at 17:42
1

As Mark Kaplun said in the comments, something is adding defer to your one domain's script.

If you want your subdomain jQuery script to also have defer, this should do it:

function my_add_defer ($tag, $handle, $src) {
    if ($handle == 'jquery') {
        return str_replace (' src=', ' defer="defer" src=', $tag);
    }
    return $tag;
}
add_filter ('script_loader_tag', 'my_add_defer', 10, 3);
0

This is the one, that caused the difference and that i was missing:

function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;

return "$url' defer='defer";
  }
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.