HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML async Attribute
Photo Credit to CodeToFun
🙋 Introduction
The async
attribute is a valuable feature in HTML that is specifically used with script elements.
It controls whether the associated script should be executed asynchronously or not.
This attribute can significantly impact the loading and execution behavior of scripts in your web page.
🎯 Purpose of async
The primary purpose of the async
attribute is to influence the way scripts are loaded and executed.
When the async
attribute is present, it allows the script to be downloaded asynchronously, meaning it won't block the rendering of the rest of the page.
This is particularly useful for non-essential scripts that don't depend on the page structure.
💎 Values
The async
attribute has a boolean value, and it can be set to either:
- true: This indicates that the script should be executed asynchronously.
- false (default): If the attribute is not present or set to false, the script will be executed synchronously, and the browser will wait for the script to complete before continuing to parse the HTML and rendering the page.
📄 Example:
Let's look at a simple example of how to use the async
attribute in an HTML script element:
<script async src="myscript.js"></script>
🧠 How it Works
In this example, the async
attribute is set to true, indicating that the script myscript.js should be loaded and executed asynchronously.
🔄 Dynamic Values with JavaScript
Similar to other HTML attributes, you can also dynamically set the async
attribute using JavaScript.
This can be useful when you need to change the loading behavior of scripts based on certain conditions or user interactions. Here's a brief example:
<script>
// Dynamically set async attribute for a script element
var scriptElement = document.createElement("script");
scriptElement.src = "dynamicScript.js";
scriptElement.async = true;
document.head.appendChild(scriptElement);
</script>
🧠 How it Works
In this script, a new script element is created dynamically, and the async
attribute is set to true before appending it to the head of the document. This allows the script dynamicScript.js to be loaded asynchronously.
🏆 Best Practices
- Use the
async
attribute for scripts that are non-essential and don't depend on the structure of the page. - Be cautious when using async for scripts that rely on the order of execution, as the asynchronous loading may lead to unexpected behavior.
- Always test your web page to ensure that the asynchronous loading of scripts doesn't negatively impact the functionality of your site.
🎉 Conclusion
The async
attribute is a powerful tool for optimizing the loading and execution of scripts in HTML.
By understanding how and when to use this attribute, you can improve the performance and user experience of your web pages.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML async Attribute), please comment here. I will help you immediately.