Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ HTML Script Element Attributes: async vs. defer vs. type='module'


๐Ÿ“š HTML Script Element Attributes: async vs. defer vs. type='module'


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

I believe you are no stranger to script elements in HTML. It has some properties such as async, defer, etc. Do you know what they do and the difference between them?

If the script element does not have any attributes added, then when the browser encounters the script, it will be loaded and executed immediately. After waiting for the execution to complete, it will continue to parse the following tags. This stage is called Parse HTML.

And if the script is large or time-consuming to execute, the page will be blank for a long time. Just like the screenshot above. This is especially common in some SPA front-end projects.

So for this situation, we can add attributes to script elements to optimize. Next, I divide script elements into three categories to explain separately:

Classic scripts

When we omit the type attribute for script elements or configure as JavaScript MIME type (the only valid value currently text/javascript), the script is a classic script.

<script type="text/javascript" async></script>

When the async attribute is configured for a classic script, the script is fetched immediately but does not block parsing HTML. Once the fetch is complete , it will be executed immediately, and if the HTML is not parsed at this time, it will be blocked.

<script type="text/javascript" src="/test.js" defer></script>

When the defer attribute is configured for a classic script, the script is fetched immediately but does not block parsing HTML. It is not executed until the HTML is parsed, but it blocks the DOMContentLoaded event.

It should be noted that if this classic script does not use the src attribute, that is, an inline script, the defer attribute will have no effect.

Module scripts

When we configure type="module" for a script element, then this script is a module script.

<script type="module"></script>

ESM can be used inside a module script, and even if the same module script is loaded multiple times, the script will only be executed once.

In the loading mechanism, its effect is the same as the defer of the classic script.

It is worth noting that if the module script is configured with the src attribute, then unlike the classic script (which allows cross-domain), this module script needs to use the CORS protocol for cross-domain fetching.

<script type="module" async></script>

When the async attribute is configured for a module script, its loading mechanism is the same as the classic script configured with async.

<script type="module" defer></script>

When the defer attribute is configured for a module script, this attribute has no effect. Because module scripts are โ€œdeferโ€ by default.

Other

When the type attribute value of the script element is not the above two valid values. Then the content in this script element (including src) will be discarded by the browser and will not be downloaded and executed.

Conclusion

The screenshot below clearly shows the difference between these types:

If you find this helpful, please consider subscribing to my newsletter for more insights on web development. Thank you for reading!

...



๐Ÿ“Œ HTML Script Element Attributes: async vs. defer vs. type='module'


๐Ÿ“ˆ 94.23 Punkte

๐Ÿ“Œ Describe the difference between `<script>`, `<script async>` and `<script defer>` for Optimal Website Performance


๐Ÿ“ˆ 60.05 Punkte

๐Ÿ“Œ Describe the difference between `<script>`, `<script async>` and `<script defer>` for Optimal Website Performance


๐Ÿ“ˆ 60.05 Punkte

๐Ÿ“Œ JavaScript Performance โ€“ How to Improve Page Speed with async and defer


๐Ÿ“ˆ 38.86 Punkte

๐Ÿ“Œ Async vs Defer in JavaScript: Which is Better?๐Ÿค”


๐Ÿ“ˆ 38.86 Punkte

๐Ÿ“Œ Saving variation attributes on new products and with attributes containing slashes could lead to XSS


๐Ÿ“ˆ 32.67 Punkte

๐Ÿ“Œ [Crosspost] Overwatch - Avoid "async" option for DXVK - Banned for "hacking" [some users may have been banned without async enabled]


๐Ÿ“ˆ 31.07 Punkte

๐Ÿ“Œ Medium CVE-2020-28490: Async-git project Async-git


๐Ÿ“ˆ 31.07 Punkte

๐Ÿ“Œ Asyncโ€ฆ oh, wait (Introduction into Async/Await)


๐Ÿ“ˆ 31.07 Punkte

๐Ÿ“Œ Is async/await a good idea? ๐Ÿค” async/await vs promises


๐Ÿ“ˆ 31.07 Punkte

๐Ÿ“Œ Mastering Async/Await: Simplifying JavaScript's Async Operations


๐Ÿ“ˆ 31.07 Punkte

๐Ÿ“Œ CVE-2022-45122 | Movable Type 7/Type Premium/Type Premium Advanced cross site scripting


๐Ÿ“ˆ 25.12 Punkte

๐Ÿ“Œ CVE-2022-45113 | Movable Type 7/Type Premium/Type Premium Advanced URL input validation


๐Ÿ“ˆ 25.12 Punkte

๐Ÿ“Œ CVE-2022-43660 | Movable Type 7/Type Premium/Type Premium Advanced os command injection


๐Ÿ“ˆ 25.12 Punkte

๐Ÿ“Œ Python Type Hinting: From Type Aliases To Type Variables and New Types


๐Ÿ“ˆ 25.12 Punkte

๐Ÿ“Œ HTML Tips - Less Known HTML Tags and Attributes โ™ฅ๏ธ


๐Ÿ“ˆ 24.86 Punkte

๐Ÿ“Œ How to Split Attributes of Same Type in One Row and Convert Them into Multiple Rows


๐Ÿ“ˆ 24.71 Punkte

๐Ÿ“Œ Element N.v Element Instantshop 1.0 add_2_basket.asp price privilege escalation


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ CVE-2016-2821 | Mozilla Firefox up to 46 DOM Element mozilla::dom::Element use after free (RHSA-2016:1217 / Nessus ID 91546)


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ CVE-2022-22512 | VARTA Element Backup/Element S Web UI hard-coded credentials (VDE-2022-061)


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferรผberlauf


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ Microsoft Internet Explorer 9/10 SVG Element Text Element memory corruption


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ how i can click a child element inside of element ?


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferรผberlauf


๐Ÿ“ˆ 23.65 Punkte

๐Ÿ“Œ Open-Xchange OX AppSuite up to 7.8.0-rev26 defer Servlet Open Redirect


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Start, Stop, Defer; Adapting to a Crisis; and Building a Culture - BSW #169


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Microsoft removed the defer feature update setting in Windows 10


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Testing Defer Blocks in Angular with Cypress


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Doubt with the defer in GOLang


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Made a simple program to defer BadUSBs by blocking input until Ctrl+Alt+Delete is pressed.


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Total guide to lazy loading with Angular @defer


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Open-Xchange OX AppSuite bis 7.8.0-rev26 defer Servlet Redirect erweiterte Rechte


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Angular @defer Interactive Examples


๐Ÿ“ˆ 23.32 Punkte

๐Ÿ“Œ Open-Xchange OX AppSuite bis 7.8.0-rev26 defer Servlet Redirect erweiterte Rechte


๐Ÿ“ˆ 23.32 Punkte











matomo