Script defer Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Script

Description

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

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

The defer attribute is only for external scripts.

If only async is present: The script is executed asynchronously with the rest of the page

If only defer is present: The script is executed when the page has finished parsing

If neither async or defer is present: The script is fetched and executed immediately before parsing the page

Set the defer property with the following Values

Value Description
true|false Sets whether a script should be executed when the page has finished parsing

Return Value

A Boolean, returns true if the script is executed when the page has finished parsing, otherwise it returns false

The following code shows how to check if a script was executed when a page was finished parsing:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<script id="myScript" src="demo_defer.js" defer></script>

<p id="p1">Hello World!</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from www . j a  v  a 2s.c om
    var x = document.getElementById("myScript").defer;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials