<script async src="siteScript.js" onload="myInit()"><script>
<script defer src="siteScript.js" onload="myInit()"><script>
Paste the code below in the your functions.php (child’s theme).
If you want to use defer just replace async=’async’ with defer=’defer’.
Deferred – delays the execution of the script until the HTML parser has finished.
Asynchronous – executes when the script is ready and doesn’t disrupt HTML parsing.
Difference between async and defer
The difference between async and defer centers around when the script is executed. Each async script executes at the first opportunity after it is finished downloading and before the window’s load event. This means it is possible that async scripts are not executed in the order in which they occur in the page. In the past, async lacked of support in older browsers.
In contrast, the defer scripts are guaranteed to be executed in the order they occur in the page. That execution starts after parsing is completely finished, but before the document’s DOMContentLoaded event. WebKit-based browsers, Firefox has long supported the defer and onload attributes and support for async was added in version 3.6. Internet Explorer has also long supported the defer attribute.
Which one is better? I will go for async for sure! Since WebKit had implemented async will speed up the websites a lot.
What do you think?