diff --git a/files/en-us/learn/javascript/first_steps/what_is_javascript/async-defer.jpg b/files/en-us/learn/javascript/first_steps/what_is_javascript/async-defer.jpg new file mode 100644 index 000000000000000..639914362df0683 Binary files /dev/null and b/files/en-us/learn/javascript/first_steps/what_is_javascript/async-defer.jpg differ diff --git a/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html b/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html index c46d425f0ca3d00..86d624ece535f97 100644 --- a/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html +++ b/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html @@ -335,7 +335,19 @@
There are actually two modern features we can use to bypass the problem of the blocking script — async
and defer
(which we saw above). Let's look at the difference between these two.
Scripts loaded using the async
attribute (see below) will download the script without blocking rendering the page and will execute it as soon as the script finishes downloading. You get no guarantee that scripts will run in any specific order, only that they will not stop the rest of the page from displaying. It is best to use async
when the scripts in the page run independently from each other and depend on no other script on the page.
Scripts loaded using the async
attribute will download the script without blocking the page while the script is being fetched. However, once the download is complete, the script will execute, which blocks the page from rendering. You get no guarantee that scripts will run in any specific order. It is best to use async
when the scripts in the page run independently from each other and depend on no other script on the page.
Scripts loaded with the defer
attribute will load in the order they appear on the page. They won't run until the page content has all loaded, which is useful if your scripts depend on the DOM being in place (e.g. they modify one of more elements on the page).
Here is a visual representation of the different script loading methods and what that means for your page:
+ + + +For example, if you have the following script elements:
@@ -357,12 +369,14 @@All the scripts with the defer
attribute will load in the order they appear on the page. So in the second example, we can be sure that jquery.js
will load before script2.js
and script3.js
and that script2.js
will load before script3.js
. They won't run until the page content has all loaded, which is useful if your scripts depend on the DOM being in place (e.g. they modify one of more elements on the page).
In the second example, we can be sure that jquery.js
will load before script2.js
and script3.js
and that script2.js
will load before script3.js
. They won't run until the page content has all loaded, which is useful if your scripts depend on the DOM being in place (e.g. they modify one of more elements on the page).
To summarize:
async
and defer
both instruct the browser to download the script(s) in a separate thread, while the rest of the page (the DOM, etc.) is downloading, so the page loading is not blocked by the scripts.async
and defer
both instruct the browser to download the script(s) in a separate thread, while the rest of the page (the DOM, etc.) is downloading, so the page loading is not blocked during the fetch process.async
attribute will execute as soon the download is done. This blocks the page and does not guarantee any specific execution order.defer
attribute will load in the order they are in and will only execute once everything has finished loading.async
.defer
and put their corresponding <script>
elements in the order you want the browser to execute them.