Script async Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Script

Description

The async property sets or gets whether a script should be executed asynchronously.

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

The async 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 async property with the following Values

Value Description
true|false Sets whether a script should be executed asynchronously

Return Value

A Boolean, returns true if the script is executed asynchronously, otherwise it returns false

The following code shows how to check if a script was executed asynchronously as soon as it was available:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p id="p1">Hello World!</p>
<script id="myScript" src="demo_async.js" async></script>

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

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

<script>
function myFunction() {//from  ww w  .  ja  va2s .  c  o  m
    var x = document.getElementById("myScript").async;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials