Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use async vs defer when loading scripts?

So I've recently learned that putting your js at the bottom of the DOM is antiquated, and that I should be once again putting them in the <head> with the “async” and “defer” attributes.

Great. But I'm a bit confused as to which I should use, based on priority.

So I have:

  • jquery
  • jquery plugins that don't have immediate effects on the look of the page
  • jquery plugins that do have immediate effects on the look of the page
  • my own personal scripts, which have immediate effects on the look of the page, and is also reliant on jquery

Which should get async, and which should get defer?

If I understand all this correctly, the ones that don't have an immediate effect on the look of the site should get defer, while everything else gets async. Correct? Or am I getting these mixed up.

like image 814
Felix Maxime Avatar asked Jun 08 '17 19:06

Felix Maxime


People also ask

What's the difference between loading sync async and defer scripts?

What's The Difference Between Async & Defer? async and defer both load JavaScript asynchronously without render blocking, but async executes as soon as possible while defer runs in sequence toward the end of the loading process, just before the DOMContentLoaded event.

What is the use of async and defer in script tag?

Async - means execute code when it is downloaded and do not block DOM construction during downloading process. Defer - means execute code after it's downloaded and browser finished DOM construction and rendering process.

Should you use defer in script tag?

You should use defer for all other scripts. defer is great because it: Gets loaded as soon as possible — so it reduces load times. Doesn't execute until everything you need is ready — so all the DOM you need is there.

What is the difference between script script async and script defer?

Async vs Defer. With async , the file gets downloaded asynchronously and then executed as soon as it's downloaded. With defer , the file gets downloaded asynchronously, but executed only when the document parsing is completed. With defer , scripts will execute in the same order as they are called.


1 Answers

It's quite simple. You should use [async] for scripts which can be executed in any order, and [defer] for scripts which have to be executed after HTML is parsed.

For example, if you have a script that add social sharing icons next to your posts, and this script doesn't rely on any other script, you can use both [async] and [defer]. But if your scripts requires jQuery, you can't use [async], because if you do, it might turn out that it gets executed before jQuery is loaded and it breaks.

If all your scripts require jQuery, then you shouldn't use [async] at all. As for [defer], it depends on whether your scripts access DOM. For plugins it probably doesn't matter, but you'll probably need it for your own code.

If you wrap your scripts in $(document).ready();, you can use [defer] for scripts which don't have immediate effect (e.g. require user interaction).

like image 137
Michał Perłakowski Avatar answered Sep 24 '22 19:09

Michał Perłakowski