You are here: Reference > JavaScript > client-side > HTML DOM > properties > defer (script)

defer property (script)

Browser support:
3.5
Sets or retrieves whether the script is going to generate any document content.
If set to true, then the browser no need to wait for the script to load. The browser will load the script in the background while continuing to process the page.
Note that the defer property is supported in Firefox from version 3.5 and it is not supported for embedded script blocks, only for external script files. See the examples below.

Syntax:

object.defer;
You can find the related objects in the Supported by objects section below.
This property is read/write.
HTML page for this property: defer

Possible values:

Boolean that indicates the status of the script element.
One of the following values:
false
Default. Browser must wait for the script to load before starts processing the page.
true
Browser no need to wait for the script to load before starts processing the page.
Default: false.

Example HTML code 1:

This example illustrates the use of the defer attribute for embedded script blocks. This example only works in Internet Explorer, Firefox does not support the defer attribute for embedded script blocks:
<head>
    <script type="text/javascript" defer="true">
        alert ("This script block will be executed after content rendering time");
    </script>

    <script type="text/javascript">
        alert ("This script block will be executed during content rendering time");
    </script>
</head>
Did you find this example helpful? yes no

Example HTML code 2:

This example is similar to the previous one, but it works both in Internet Explorer and Firefox (from version 3.5) because it uses the defer attribute for external script files:
Code
after.js
during.js
<head>
    <script type="text/javascript" src="after.js" defer="true"></script>

    <script type="text/javascript" src="during.js"></script>
</head>
Did you find this example helpful? yes no

Example HTML code 3:

This example illustrates the use of the defer property in JavaScript:
<head>
    <script type="text/javascript" id="script1" defer="true">
        alert ("This script block will be executed after design time");
    </script>

    <script type="text/javascript" id="script2">
        alert ("This script block will be executed during design time");
    </script>

    <script type="text/javascript">
        function Init () {
            var script1 = document.getElementById ("script1");
            var script2 = document.getElementById ("script2");
            alert ("The defer state of the first script: " + script1.defer);
            alert ("The defer state of the second script: " + script2.defer);
        }
    </script>
</head>
<body onload="Init ();">
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content