async

HTML async Attribute (HTML异步属性)

The async attribute is a boolean attribute and specifies that the script will be executed asynchronously once it is available. It only works for external scripts and must be used only when the src attribute is present. (Async属性是一个布尔属性,指定脚本在可用时将异步执行。它仅适用于外部脚本,必须仅在存在src属性时使用。)

You can use the async attribute on the <script> element.

An external script can be executed in the following ways: (外部脚本可以通过以下方式执行:)

  • When async is present, the script will be executed asynchronously while the page continues the parsing. (-当异步存在时,脚本将异步执行,同时页面继续解析。)

  • When async is not present but defer is present, the script will be executed when the page finishes the parsing. (-当async不存在但defer存在时,脚本将在页面完成解析时执行。)

  • When neither async or defer is present, the script will be executed immediately before the browser continues the parsing. (-当不存在异步或延迟时,脚本将在浏览器继续解析之前立即执行。)

Syntax

Syntax (语法)

<script src="example.js" async></script>

Example of the HTML async attribute:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the documnet</title>
   <script src="example.js" async></script>
   <noscript>Sorry, your browser does not support JavaScript!</noscript>
 </head>
 <body>
   <h1>Example</h1>
   <p>
     A browser that doesn’t support JavaScript will display the content inside the noscript element.
(不支持JavaScript的浏览器将显示noscript元素中的内容。)
   </p>
   <script>
     document.write("My first JavaScript example!")
(document.write ("我的第一个JavaScript示例!"))
   </script>
 </body>
</html>


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部