Asynchronous Script

Description

The default browser behavior when it encounters a script element is to stop processing the page while it loads and executes the script.

Each script element is executed synchronously in the order in which they are defined, nothing else happens when the script is loading and running.

You can improve performance by using the async attribute. For example, a self-contained script, which does not interact with the elements in the HTML document, can be executed asynchronously.

When you use the async attribute, the browser loads and executes the script asynchronously while it continues to parse the other elements in the HTML, including other script elements.

Example

The following code shows the async attribute applied to a script element.


<!DOCTYPE HTML>
<html>
<head>
   <script async  src="simple2.js"></script>
</head>
<body>
    <p>This is a test.</p>
    <a href="http://java2s.com">java2s.com</a>
</body>
</html>

Note

The scripts with async attribute might not be executed in the order in which they are defined.

This makes the async feature unsuitable for scripts that depend on functions or values defined by other scripts.





















Home »
  HTML CSS »
    HTML »




HTML Introduction
HTML Document
HTML Section
HTML Group Content
HTML Text Marker
HTML Table
HTML Form
HTML Embed