Atributo async de HTML
El atributo async es un atributo booleano y especifica que el script se ejecutará de forma asíncrona una vez que esté disponible. Solo funciona para scripts externos y debe usarse únicamente cuando el atributo src esté presente.
Puedes usar el atributo async en el elemento <script>.
Un script externo puede ejecutarse de las siguientes maneras:
- Cuando está presente
async, el script se ejecutará de forma asíncrona mientras la página continúa su análisis. - Cuando no está presente
asyncpero sídefer, el script se ejecutará cuando la página termine su análisis. - Cuando ni
asyncnideferestán presentes, el script se ejecutará inmediatamente antes de que el navegador continúe con el análisis.
Sintaxis
html
<script src="example.js" async></script>Ejemplo del atributo async de HTML:
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</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.
</p>
<script>
document.write("My first JavaScript example!")
</script>
</body>
</html>Práctica
¿Cuál es la función del atributo 'async' en HTML?