David Flanagan - JavaScript. Detailed guide:
Both attributes, defer
and async
, tell the defer
that the script does not
uses the document.write()
method and does not generate the contents of the document, and that the browser can continue parsing and displaying the document while the script is loading.
The defer
attribute causes the browser to postpone the execution of the script until the document is loaded, analyzed and ready to perform operations. The async
attribute causes the browser to execute the script as soon as it becomes possible, but does not block parsing the document while the script is loading.
If the <script>
has both attributes, the browser supporting both of these attributes will prefer the async
attribute and ignore the defer
attribute.
Note that deferred scripts are executed in the order in which they appear in the document . Asynchronous scripts run as soon as they are loaded, that is, they can be executed in any order .
Thus, I advise you to leave all the scripts in the header (why spoil the code), and everywhere where jquery is used, and the connection of the library itself to register the attribute defer
. Scripts where jquery is not used (that is, they do not depend on the connection of other scripts) can be connected with the async
attribute, for the fastest possible download. Postponed scripts (those with defer
) must be defer
starting with jquery so that there are no errors.
In general, I personally always use defer
- it does not block the download and you know exactly the sequence of execution (the DOM model is already loaded and you don’t need to use window.onload
).
async
from jQuery. - Visman