Use the async attribute to load and execute JavaScript asynchronously

One of the biggest things that excites me about HTML5 is that it implements new features and features that we’ve been waiting for for a long time. For example, I’ve been using placeholders before, but had to do it in JavaScript. The async attribute provided to JavaScript tags in HTML5 enables JavaScript to be loaded and executed asynchronously. Previously I needed various JavaScript plugins to implement this functionality, but now this new property allows us to easily implement asynchronous loading.

async – HTML code

It’s really very simple, like this:

 <script async src="siteScript.js" onload="myInit()"></script>

In fact, if you’re a serious programmer, you should use the async attribute on over 90% of your SCRIPT tags.

defer – HTML code

There is also a defer attribute similar to the async attribute:

 <script defer src="siteScript.js" onload="myInit()"></script>

The syntax is very similar to that of the async attribute.

async & defer – the difference

This WebKit blog will…

The post Loading and Executing JavaScript Asynchronously Using the async Attribute first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9948
This site is for inclusion only, and the copyright belongs to the original author.