#45130: Defer jQuery WordPress Customizer doesn't work property
-------------------------------+------------------------------
Reporter: remzicavdar | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Customize | Version:
Severity: normal | Resolution:
Keywords: reporter-feedback | Focuses:
-------------------------------+------------------------------
Comment (by remzicavdar):
Hi @dlh
I am sorry to have kept you waiting for so long. I live in a different
time zone.
I use the following code:
{{{#!php
<?php
function add_defer_attribute($tag, $handle) {
// add script handles to the array below
$scripts_to_defer = array('bootstrap-bundle-js');
$scripts_to_defer = array('jquery', 'jquery-migrate', 'bootstrap-
bundle-js');
foreach($scripts_to_defer as $defer_script) {
if ($defer_script === $handle) {
return str_replace(' src', ' defer="defer" src', $tag);
}
}
return $tag;
}
add_filter('script_loader_tag', 'add_defer_attribute', 10, 2);
}}}
It is also good practice to put all the scripts into the footer, I
understand that this has been discussed before. Even when the team decides
not to, it's best to use document ready, otherwise everything breaks if
jQuery has been placed in the footer. If a theme developer decides to put
jQuery in the footer in his or her theme. In this case, we would preface
the anonymous function with a document ready function as seen below:
{{{
jQuery(document).ready(function($) {
// Your jQuery code
});
}}}
I think using document ready is always a good thing to do, even if you put
your code in the header.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/45130#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform