Find out if a script was executed asynchronously when it was available:
var x = document.getElementById("myScript").async
Click the button to find out if the script was executed asynchronously when it was available.
<!DOCTYPE html> <html> <body> <p id="p1">Hello World!</p> <script id="myScript" src="javascript.js" async></script> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from ww w .ja v a 2s . c om var x = document.getElementById("myScript").async; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The async property sets or gets whether a script should be executed asynchronously when it is available.
This property mirrors the async attribute of the <script> tag.
The async 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 async property accepts and returns a boolean type value.
Value | Description |
---|---|
true | The script will be executed asynchronously as soon as it is available |
false | The script will not be executed asynchronously as soon as it is available |
The async property returns true if the script is executed asynchronously when it is available, otherwise it returns false.