πŸ“„
Performance

JavaScript defer and async loading explained

πŸ“… 14 October 2026 ✏️ 14 October 2026 ⏱ 5 min leestijd

Learn the difference between defer and async for loading JavaScript and how to speed up your website without losing functionality.

Scripts that load in a blocking manner are a common cause of slow websites. The defer and async attributes solve this by loading JavaScript in a smarter way, without the browser having to wait before the page becomes visible. In this article, we explain the difference, when to use which, and how to apply this to your own website.

Why JavaScript blocks your page

A browser reads HTML from top to bottom. When it encounters a <script> tag without extra attributes, building the page stops completely until the script has been downloaded and executed. For a script in the <head>, this means visitors stare at a blank screen while the browser waits for a file that may have nothing to do with the visible content.

This problem grows as you use more scripts: analytics, chat widgets, cookie banners, and theme functionality pile up. Every script that loads synchronously adds delay before the page becomes usable.

The difference between defer and async

Both attributes ensure that the browser downloads a script in the background while the rest of the page continues building normally. The difference lies in the moment the script is executed.

async executes the script as soon as it finishes downloading, regardless of whether the rest of the page has already loaded. This can briefly interrupt HTML parsing, and scripts are not guaranteed to run in the order they appear in the HTML.

defer waits to execute until the entire HTML has loaded, then runs scripts in the order they were placed, just before the DOMContentLoaded event fires. This makes defer more predictable and safer for scripts that depend on each other or need to modify the DOM.

Parsing HTMLScript downloadasync: run immediatelydefer: after parsingPage visible

When to use which

Use async for scripts that work independently and don’t wait on other scripts or the DOM, such as analytics tools or advertising code. Execution order usually doesn’t matter here.

Use defer for scripts that modify page content, depend on each other, or need to run after the DOM is fully built. This applies to most theme scripts, form validation, and interactive elements.

If you add no attribute at all, the script will always block the page. This is only desirable for very small, critical scripts that directly affect the initial render.

πŸ’‘ Tip: Always test changes on a staging environment first. An incorrectly placed defer or async attribute can cause a script to run too early or too late, resulting in broken functionality.

Applying this to your own website

Adding these attributes is usually a small change in the HTML or in your CMS or theme settings. Follow the steps below to implement this carefully.

1

Inventory your scripts

Check which scripts your page loads and whether they depend on each other or on the DOM.

2

Choose the right attribute

Add defer to scripts that affect the page and async to independent scripts such as tracking code.

3

Adjust the order if necessary

When using defer, the order in the HTML is preserved, so place dependent scripts in the correct order.

4

Test the page thoroughly

Check whether all interactive elements, forms, and widgets still work correctly after the change.

5

Measure the result

Use the browser console or a speed test to verify that load time has actually improved.

If you run on a LiteSpeed server, you can easily edit files and clear the cache via Plesk so that changes become visible immediately. If you’re unsure about the right approach, check the knowledge base or contact support. If you manage multiple sites with associated domains, also check out our page on domain names and web hosting for a suitable hosting environment.

Frequently asked questions

Can I use defer and async together on one script?

No, if both attributes are present, modern browsers ignore defer and the script behaves as async.

Do defer and async work in all browsers?

Both attributes are widely supported by modern browsers. Older browsers may ignore async, in which case the script behaves as a normal blocking script.

Does defer affect inline scripts?

No, defer only works on scripts that load an external file via the src attribute, not on inline JavaScript between the script tags.

Can applying defer incorrectly break my site?

Yes, if a script depends on elements that haven’t loaded yet or on a different order, functionality can stop working. Always test thoroughly.

Does this solve all speed problems?

No, defer and async only improve how scripts are loaded. Large images, slow servers, or heavy plugins require a separate approach.

Conclusion

By loading scripts with defer or async, you prevent JavaScript from blocking your page’s construction. Use defer for scripts that depend on the DOM or on each other, and async for independent scripts. Always test changes carefully before going live.

View web hosting β†’

Was dit artikel nuttig?