HTML <script> defer Attribute

The defer attribute on a <script> tag specifies that the script is executed after the page has loaded.

Example

#

A defer attribute on a <script> element.

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

Script file

Here is the script file. It displays an alert box when executed.

alert("This alert is executed deferred.");

Using defer

The defer attribute specifies that the script file will execute after the page has finished loading.

This attribute only works with external scripts and only when src attribute is present.

External script files can be executed in the following 3 ways:

  • no attribute - Default: the script is fetched an executed right away, even before the page loads.
  • async - Asynchronously: the script will be executed when available, as the page loads.
  • defer - Deferred: executes the script only when the page has been completely loaded.

Syntax

<script defer>

Browser support

Here is when defer support started for each browser:

Chrome
8.0 Dec 2010
Firefox
3.5 Jun 2009
IE/Edge
10.0 Sep 2012
Opera
15.0 May 2013
Safari
5.0 Jun 2010

You may also like

 Back to <script>
Guides