Loading...
Loading...
The <script> tag is used to connect JavaScript files to HTML documents. Depending on async and defer attributes, script loading and execution behavior changes.
<script src="script.js"></script>
Behavior:
<script src="script.js" defer></script>
Behavior:
DOMContentLoaded event.| Attribute | Loading | Execution | Blocks Parsing? | Preserves Order? |
|---|---|---|---|---|
(no attributes) | immediately | immediately after load | Yes | Yes |
async | parallel | immediately after load | No (but can block execution) | No |
defer | parallel | after HTML parsing | No | Yes |
Tip:
If you're not using type="module", always prefer defer for internal scripts — it doesn't block HTML and preserves order.