Javascript Reference - HTML DOM Script defer Property








The defer property sets or gets whether a script should be executed when a page has finished loading.

This property reflects the defer attribute of the <script> tag.

Browser Support

defer Yes Yes Yes Yes Yes

Syntax

Return the defer property.

var v = scriptObject.defer 

Set the defer property.

scriptObject.defer=true|false




Property Values

Value Description
true|false Set whether to run a script after the page has finished loading

Return Value

A Boolean type value, true if the script is executed when the page has finished parsing, otherwise false.

Example

The following code shows how to get if a script was executed when a page was finished parsing.


<!DOCTYPE html>
<html>
<body>
<script id="myScript" src="demo_defer.js" defer></script>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from ww  w  .  ja va2s.  com-->
<script>
function myFunction() {
    var x = document.getElementById("myScript").defer;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: