HTML - attributes - defer

revision:


Content

"defer" attribute : boolean attribute for external scripts syntax some examples


"defer" attribute : boolean attribute for external scripts

top

When present, it specifies that the script is executed when the page has finished parsing. The defer attribute is only for external scripts (should only be used if the "src attribute" is present). The defer attribute can be used on the <script> element.

By default, HTML documents are parsed (read) from top to bottom, one line at a time. This means that if you put any JavaScript at the top of your document, it will execute before the rest of your document is done parsing. That's the default behavior of the script element. But when you add "defer" to the <script> element you disable that default behavior.


syntax

top

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

The defer attribute doesn't have an assignment operator (=) or value because its behavior is built-in. The JavaScript code won't execute until the entire page is finished loading.
In a literal sense, defer means put off/postpone/wait. Without "defer", JavaScript executes as soon as it is loaded. With "defer", JavaScript waits to execute until the entire HTML page is loaded.


some examples

top

example

HTML codes:

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