<script> Attributes: defer

By: Dusty Arlia
Published on Monday, March 17, 2014, 07:59 PM
Last Updated on Tuesday, July 07, 2015 at 8:36 PM
Total Updates: 4

The HTML <script> tag has a defer attribute that mirrors the defer property of the Script JavaScript object. The defer attribute is a boolean attribute. What this means is it doesn't have a value, it just needs to be present. If the defer attribute is present, then the script will run after the document has been loaded and parsed and is ready to be manipulated. These scripts run in the order in which they appear in the document (compared to asynchronous scripts which can run out of order).

If the defer attribute is not present (and neither is a async attribute), it will run synchronously (the default, also called a blocking script). If a script is running asynchronouly, then it means other things can be running at the same time.

According to the HTML5 standard, the defer attribute and the async attribute can only be used when the src attribute is used. Some browsers may support deferred inline scripts. If both the async and defer attributes are present, the browser will use the async attribute and ignore the defer atttribute.