---
title: "What Is Defer JavaScript? Performance Glossary | Seahawk"
description: "Deferring JavaScript means using the defer attribute so a script downloads in the background and runs only after the HTML is parsed."
url: https://seahawkmedia.com/glossary/defer-javascript/
language: en
---

[Home](https://seahawkmedia.com/) / [Glossary](https://seahawkmedia.com/glossary/) / [Performance](https://seahawkmedia.com/glossary/performance/) /Defer JavaScript

# Defer JavaScript

September 7, 2022 Updated November 16, 2026 2 min read

Performance glossaryDeferring JavaScript means using the defer attribute so a script downloads in the background and runs only after the HTML is parsed. It no longer blocks the page.

Deferring JavaScript means adding the `defer` attribute to a script tag:

`<script src="/js/menu.js" defer></script>`

Without it, the browser stops reading the HTML whenever it meets a script, downloads and runs the script, and only then continues. With `defer`, the script downloads in the background while the HTML is parsed, and runs once parsing is complete, just before the `DOMContentLoaded` event. The visible page can be built and painted without waiting for it.

## Defer vs async

|  | Normal script |  5  |  6  |
| --- | --- | --- | --- |
| Blocks HTML parsing | Yes, while it downloads and runs | Only while it runs | No |
| When it runs | Immediately | As soon as it downloads | After the HTML is parsed |
| Keeps script order | Yes | No | Yes |

Because deferred scripts keep their order, `defer` is the safer choice for scripts that depend on each other, such as jQuery and the code built on it. `async` suits independent scripts such as analytics. There is more on the difference under [asynchronous loading](https://seahawkmedia.com/glossary/asynchronous-loading/).

Two details catch people out: `defer` only works on external scripts with a `src`, not on inline scripts, and JavaScript modules (`type="module"`) are deferred automatically.

## Why it matters

Scripts in the `<head>` without `defer` or `async` are render-blocking: nothing appears until they have downloaded and run. [PageSpeed Insights](https://seahawkmedia.com/glossary/pagespeed-insights/) flags them as render-blocking resources; older tools called the fix "defer parsing of JavaScript". Deferring them usually improves First Contentful Paint and [Largest Contentful Paint](https://seahawkmedia.com/glossary/largest-contentful-paint/).

Deferring does not reduce how much JavaScript the page runs, so it does less for [Interaction to Next Paint](https://seahawkmedia.com/glossary/interaction-to-next-paint/). For that, the scripts themselves have to get smaller or fewer.

## How to defer JavaScript in WordPress

### In a theme or plugin

Load scripts with `wp_enqueue_script()` and pass a loading strategy:

```php
wp_enqueue_script( 'my-menu', get_theme_file_uri( 'js/menu.js' ), array(), '1.0', array( 'strategy' => 'defer', 'in_footer' => true ) );
```

WordPress adds the attribute but checks the dependency tree first: if a script that relies on this one would still load normally, WordPress keeps this one blocking too, so nothing runs out of order.

### With a plugin

Performance plugins such as WP Rocket, Autoptimize, and LiteSpeed Cache can defer scripts site-wide, with options to exclude specific files. Some also offer to delay JavaScript until the visitor first interacts with the page, which is a different and more aggressive technique.

## Common mistakes

- Deferring jQuery while inline scripts in the page still call it immediately. The result is "jQuery is not defined" errors in the console and broken sliders, menus, or forms. Exclude jQuery, or make sure the inline code runs after it.

- Deferring scripts that must run before the page appears, such as some consent or A/B testing tools, which can cause a flash of the wrong content.

- Not testing. After any change, check key templates, forms, and checkout with the browser console open.

Source: [MDN: the script element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script).

WP Services

## WordPress services at revolutionary pricing.

Productized WordPress services, from builds to care, delivered by Seahawk and tracked in one app. For the WordPress lovers.

[Go to WP Services](https://wpservices.com/) [Free homepage rebuild](https://seahawkmedia.com/free-homepage-rebuild/)

More Performance terms

- [First Input Delay (FID)](https://seahawkmedia.com/glossary/first-input-delay/)
- [Gzip](https://seahawkmedia.com/glossary/gzip/)
- [HTTP/2](https://seahawkmedia.com/glossary/http-2/)
- [Content Delivery Network (CDN)](https://seahawkmedia.com/glossary/cdn/)
- [Core Web Vitals](https://seahawkmedia.com/glossary/core-web-vitals/)
- [Cumulative Layout Shift (CLS)](https://seahawkmedia.com/glossary/cumulative-layout-shift/)
