HTML and script and defer properties

HTML script defer property HTML and script.html

The defer property states that you should wait until all HTML document resolution is complete before judging whether the js script is executed, refer to the following example:

The script will not run until the page has been loaded:

<script src="demo_defer.js"defer></script>

Try it out . . .

Browser support

HTML script defer property HTML script defer property HTML script defer property HTML script defer property HTML script defer property

The defer property is supported by all major browsers.


Definitions and usages

The defer property is a Boolean property.

The defer property states that the script is not executed until the page has finished loading.

Note: The defer property applies only to external scripts (only when using the src property).

Note: There are several ways to execute an external script:

  • If async is "async": The script executes asynchronously relative to the rest of the page (the script is executed as the page continues to parse)
  • If you do not use async and defer is "defer": the script will execute when the page is resolved
  • If you do not use async or defer: Read and execute the script immediately before the browser continues to parse the page

Tip: If your script doesn't change the contents of the document, you can add the defer property to the label to speed up the processing of the document. Because the browser knows that it will be able to safely read the rest of the document without executing the script, it will defer the interpretation of the script until the document has been displayed to the user.


The difference between HTML 4.01 and HTML5

No.


The difference between HTML and XHTML

In XHTML, properties are prohibited from minimizing, and the defer property must be defined as the "defer" of the "script defer".


Grammar

<script defer>

HTML script defer property HTML and script.html