Javascript DOM HTML Script defer Property get

Introduction

Find out if a script was executed when a page was finished parsing:

var x = document.getElementById("myScript").defer

View in separate window

<!DOCTYPE html>
<html>
<body>

<script id="myScript" src="javascript.js" defer></script>
<p id="p1">Hello World!</p>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*  w ww. j ava 2  s. com*/
  var x = document.getElementById("myScript").defer;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

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

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

The defer attribute is only for external scripts.

There are several ways to run an external script:

Type Meaning
async The script is executed asynchronously while the page continues the parsing
defer The script is executed when the page has finished parsing
neither async or defer The script is fetched and executed immediately, then the browser continues parsing the page

The defer property accepts and returns a boolean type value.

Value Description
true The script is executed when the page has finished parsing
false The script will not be executed when the page has finished parsing

The defer property returns true if the script is executed when the page has finished parsing, otherwise it returns false.




PreviousNext

Related