defer
On this page
HTML defer Attribute (HTML延迟属性)
The HTML defer attribute specifies that a script is executed when a page has finished the parsing. It is a boolean attribute. This attribute works for external scripts and must be used only when the src attribute is present. (HTML延迟属性指定在页面完成解析时执行脚本。它是一个布尔属性。此属性适用于外部脚本,只能在存在src属性时使用。)
There isn’t any difference between HTML 4.01 and HTML5. In XHTML, the defer attribute must be specified as <script defer=”defer”> as attribute minimization is forbidden.
You can use this attribute on the <script> element.
An external script can be executed in the following ways: (外部脚本可以通过以下方式执行:)
When async is present, the script will be executed asynchronously while the page continues the parsing. (-当异步存在时,脚本将异步执行,同时页面继续解析。)
When async is not present but defer is present, the script will be executed when the page finishes the parsing. (-当async不存在但defer存在时,脚本将在页面完成解析时执行。)
When neither async or defer is present, the script will be executed immediately before the browser continues the parsing. (-当不存在异步或延迟时,脚本将在浏览器继续解析之前立即执行。)
Syntax
Syntax (语法)
<script src="example.js" defer></script>
Example of the HTML defer attribute:
<!DOCTYPE html>
<html>
<head>
<title>Title of the documnet</title>
<script src="example.js" defer></script>
<noscript>Sorry, your browser doesn't support JavaScript!</noscript>
</head>
<body>
<h1>Example</h1>
<p>The "defer" attribute specifies that a script is executed when a page has finished the parsing. It is a boolean attribute.</p>
</body>
</html>