Home > Web Front-end > JS Tutorial > JavaScript loading: defer and async

JavaScript loading: defer and async

藏色散人
Release: 2019-05-09 09:35:56
forward
2651 people have browsed it

JavaScript loading: defer and async

The parsing process of the page is single-threaded, but single-threaded can also be divided into synchronous and asynchronous. There are three ways to interpret script tags: default, defer and async.

<script src="script.js"></script>
Copy after login

Default mode, the browser will load and execute the specified script immediately. "Immediately" refers to before rendering all document content after the script tag, which means that it does not wait for subsequent loading of document elements. , load and execute after reading.

<script async src="script.js"></script>
Copy after login

async loading mode, the process of loading and rendering subsequent document elements, script.js is loaded asynchronously, and script.js is executed synchronously after script.js is loaded (possibly during the process of subsequent document element parsing being completed) )

<script defer src="script.js"></script>
Copy after login

defer loading mode, the process of loading and rendering subsequent document elements, script.js is loaded asynchronously, and script.js is executed synchronously after script.js loading is completed and subsequent document element parsing is completed

JavaScript loading: defer and async

Script tag parsing time graph

Only the two attributes of the script tag linked with the src attribute are effective.

<script defer type="text/javascript" src=""></script>
Copy after login

Script tags without the src attribute are loaded sequentially when the document is loaded. These two attributes are invalid.

<script type="text/javascript">
    let idEle = document.getElementById("app");
</script>
Copy after login

The above is the detailed content of JavaScript loading: defer and async. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:aliyun.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template