Defer JavaScript lets the browser show your page before it runs your scripts. Normally a script in the page head stops rendering until it is downloaded and executed. Deferred scripts download in the background and run, in order, once the HTML has been read. Pages appear sooner and the scripts still run on every visit.
Available in: Powered Cache (free)
Where to find it: Powered Cache → File Optimization → JavaScript
How deferring works #
When Defer JavaScript is on, Powered Cache adds the defer attribute to scripts loaded the WordPress way (enqueued scripts), including combined script files. It skips a script when:
- it already has an
asyncordeferattribute, - its tag contains
data-no-defer, - it matches Defer JavaScript Exclusions.
Inline scripts cannot be deferred by an attribute. Many of them call jQuery, which would not be loaded yet once jQuery is deferred. Powered Cache therefore wraps inline scripts that use jQuery( or $( so they run when the page has been read (on DOMContentLoaded), after the deferred jQuery. Inline scripts that already wait for DOMContentLoaded, use document.write, or contain structured data (application/ld+json) are left unchanged.
Scripts written directly into theme templates, rather than enqueued, do not get the defer attribute.
Defer or Delay? #
| Defer JavaScript | Delay JavaScript | |
|---|---|---|
| When scripts run | Right after the HTML has been read, on every visit. | Only after the visitor interacts with the page (key press, mouse movement, click, touch or mouse wheel). |
| Which scripts | Enqueued scripts, plus inline jQuery code. | All scripts in the page, external and inline. |
| Risk | Low. Content stays the same, it just appears sooner. | Higher. Anything built by JavaScript waits for interaction. |
| Good first step? | Yes. | Only after testing. |
You can turn on both. Scripts you exclude from Delay JavaScript still get deferred.
Turn on Defer JavaScript #
- Go to Powered Cache → File Optimization.
- In the JavaScript group, turn on Defer JavaScript.
- Click Save Settings and clear the cache.
- Test your pages in a private window, with the browser console open. If something breaks, add the script to the exclusions.
Settings #
Defer JavaScript #
Loads JavaScript without blocking the first rendering of the page. Default: off. Recommended setup keeps it off so you can test it first.
Defer JavaScript Exclusions #
Shown when Defer JavaScript is on. Matching scripts keep their original, blocking loading. Default: empty.
Enter one rule per line. Each rule is compared with the complete <script> tag, not just the URL, so you can match a file path, the script ID or any attribute. A rule matches when it appears anywhere in the tag.
| Rule | What it keeps from being deferred |
|---|---|
jquery-core-js | The jQuery core script, matched by the ID WordPress gives it. |
jquery.min.js | Any script whose tag contains that file name. |
/wp-content/plugins/my-slider/* | Every script of a plugin. * matches any text. |
(maps|chart)\.js | A regular expression. |
Find the right value in your page source (right-click → View Page Source) by searching for the script’s file name. A line that is not a valid regular expression is matched as plain text. You can also add data-no-defer to a script tag in your own code to keep it out.
These exclusions apply to enqueued scripts. Inline jQuery code is wrapped as described above even when it matches a rule. To keep inline code unchanged, turn off deferring for that page or use the filter in For developers.
Turn off deferring for one page #
In the post editor, open the Powered Cache panel and check Disable JS defer for this post. Neither the defer attribute nor the inline jQuery wrapping is applied to that post. The option appears while Defer JavaScript is on. See Exclude a page from the cache for all per-post options.
Troubleshooting #
The console shows “jQuery is not defined” or “$ is not a function” #
An inline script or a script written into a template runs before the deferred jQuery. Add jquery-core-js to Defer JavaScript Exclusions, clear the cache and test again.
A slider or menu appears unstyled for a moment #
The element is built by a script that now runs slightly later. Exclude that script if the short delay bothers you. See Broken layout after optimization for more steps.
For developers #
Filters: powered_cache_defer_exclusions (add rules), powered_cache_disable_js_defer (skip one script tag) and powered_cache_disable_js_defer_inline (skip the wrapping of one inline script). See the hooks reference.