Javascript examples for DOM HTML Element:Script
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 |
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:
<!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>