Defer Non-Critical JavaScript & jQuery Dependency Safety
Eliminate render-blocking <script> tags in DOM order while protecting inline jQuery calls, document.write() parser scripts, Slider Revolution, and A/B testing SDKs.
- Adds standard HTML5
deferto eligible classic<script src="...">tags (ScriptOptimizer.php) so scripts download in parallel without pausing HTML parsing, then execute in exact DOM order. - Prevents
Uncaught ReferenceError: jQuery is not definedby analyzing inline<script>blocks, WordPress dependency handles (synchronousDependencies()),ThemeCompatibility, and Slider Revolution (RS_REVISION >= 6.5.5). - Scans scripts via
ParserDependentScripts.phpto keep legacydocument.write()tags synchronous automatically. - Ships built-in
DEFAULT_EXCLUSIONSprotecting Stripe, PayPal, reCAPTCHA, Cloudflare Turnstile, hCaptcha, Cookiebot, OneTrust, Optimizely, VWO, and AB Tasty.
How Synchronous <head> Scripts Stall HTML Parsing (And How defer Fixes It)#
Whenever a browser’s HTML parser encounters a classic <script src="/wp-includes/js/jquery/jquery.min.js"></script> tag without defer or async, HTML parsing halts immediately. The browser cannot discover images further down the document, build the layout tree, or paint above-the-fold text until that script finishes downloading and executing on the main thread.
Adding the native HTML5 defer attribute changes the browser timeline cleanly:
- Non-Blocking Parallel Download: The browser fetches deferred
.jsfiles in the background at lower network priority while continuing to parse HTML and render your LCP hero banner uninterrupted. - Guaranteed Sequential Order Before
DOMContentLoaded: Unlikeasync(which executes scripts in random arrival order and breaks dependent plugins!),deferguarantees that Script 1 (jquery.min.js), Script 2 (woocommerce.min.js), and Script 3 (theme.js) execute in exact document order right after HTML parsing finishes.
Why Naive defer Plugins Cause jQuery is not defined on WordPress#
If HTML5 defer preserves order across external <script src="..."> files, why do simple code-snippet plugins that slap defer onto every script tag crash half of WordPress plugins? Because the HTML5 specification applies defer ONLY to external scripts with a src attribute—inline <script>jQuery(function($){ ... });</script> blocks ignore defer and execute immediately during HTML parsing!
If jquery-core is deferred until the end of </html>, while an inline WooCommerce widget or Slider Revolution initialization block in <body> runs immediately at line 420, the inline block crashes with Uncaught ReferenceError: jQuery is not defined.
KadiCache Free Core (ScriptOptimizer.php + ParserDependentScripts.php) inspects the entire HTML document before deferring a single script so dependency chains never break:
| Dependency Scenario Detected in HTML | How ScriptOptimizer.php Handles It Safely | Result in Browser Console |
|---|---|---|
Inline jQuery(...) / $(...) script in <body> | Coordinates jQuery scope & dependent handles (synchronousDependencies()) so inline calls never fire before jquery-core | 0 ReferenceErrors |
Slider Revolution (RS_REVISION >= 6.5.5) async hero | Detects hasAsyncHeroSlider($html) and keeps jquery ready when hero initialization requires it | Instant hero slider boot |
Script calling document.write() (ParserDependentScripts.php) | Scans local script sources & inline calls; keeps parser-dependent scripts synchronous through $nativeThrough | Legacy embeds render intact |
A/B Testing Anti-Flicker SDKs (Optimizely, VWO, AB Tasty) | Built into ScriptOptimizer::DEFAULT_EXCLUSIONS (cdn.optimizely.com, visualwebsiteoptimizer.com, abtasty.com) | Zero experiment flicker |
| Payment, Captcha & Consent SDKs | Protects js.stripe.com, paypal.com/sdk/js, recaptcha, turnstile, hcaptcha, cookiebot, cookielaw.org | Immediate checkout & GDPR banners |
When to Choose Defer JS Alone vs Defer + Delay Until Interaction#
Inside WP Admin -> KadiCache -> CSS & JavaScript, KadiCache supports both strategies cleanly (ScriptOptimizer.php even cascades automatically alongside DelayJsOptimizer.php for scripts not held by interaction delay!):
- Use
Defer Non-Critical JavaScripton Its Own When: Your site runs complex interactive dashboards, real-time financial charts, LMS quizzes, or live calculators that must initialize automatically on page load without waiting for user scroll/tap—eliminating<head>render-blocking warnings while running every script automatically right after HTML parse. - Use
Delay Until Interaction(Cascaded WithDefer) When: Your site is a WooCommerce storefront, marketing landing page, publisher blog, or agency site loading Google Tag Manager, Meta Pixel, chat widgets, and heavy UI bundles—holding non-essential third-party CPU work until interaction whileScriptOptimizercleanly defers remaining active scripts (Delay JS Guide).
Configuring Defer JS Exclusions & Developer Attributes#
Need a specific third-party anti-flicker or header script to remain synchronous in <head>?
- Defer JavaScript Exclusions (
defer_js_exclusions): Expand the Excluded Defer JS drawer inWP Admin -> KadiCache -> CSS & JavaScriptand add the script filename (ab-test-head.js), handle ID, or domain. - Excluded Defer Page Paths (
defer_js_excluded_paths): Bypass Defer JS on specific routes (/legacy-calculator/*), or check Disable: Defer JavaScript in the Gutenberg sidebar (Per-Page Exclusions). - HTML Attribute Opt-Out (
data-kadicache-no-deferordata-no-defer): Adddata-kadicache-no-deferdirectly onto any<script>tag in PHP templates:
<script src="/wp-content/themes/custom/js/critical-theme-mode.js"
data-kadicache-no-defer></script>Best Practices for Theme Developers Writing Defer-Friendly WordPress Code#
If you author custom agency themes or WooCommerce extensions, following three native WordPress standards ensures 100% zero-configuration compatibility with Defer Non-Critical JavaScript:
- Declare Explicit Script Handle Dependencies: Always pass
['jquery'](or['wp-element']/['wc-cart-fragments']) in the$depsparameter ofwp_enqueue_script()rather than printing raw<script>tags insidewp_head. - Attach Inline Initialization via
wp_add_inline_script(): WordPress associateswp_add_inline_script('my-theme', '...', 'after')directly with the parent handle soScriptOptimizer.phpsees the exact relationship. - Use
DOMContentLoadedin Standalone Vanilla JS Snippets: Wrapping standalone inline scripts indocument.addEventListener('DOMContentLoaded', function () { ... })ensures deferred libraries have finished executing before your handler runs.
Frequently Asked Operational Questions
Why does KadiCache use defer instead of async on WordPress scripts?
Does Defer JavaScript run on WooCommerce Cart and Checkout pages?
Will Defer JavaScript cause flicker if I run Optimizely, VWO, or AB Tasty A/B tests?
Does Defer JavaScript work alongside Cloud Remove Unused CSS?
What if a script uses ES Modules (<script type="module">)?
Creator of KadiCache’s Early PHP Drop-In, 7-Driver Object Cache Engine, Cloud Remove Unused CSS, and Zero-CLS Lazy Render pipeline.