How does the async attribute of Javascript work? [duplicate]

3

I was reading about the async attribute, which is used in the <script> tag, and I was curious and dubious about some things.

According to the W3School :

  

A script that will be run asynchronously as soon as it is available

Translating:

  

A script that will run asynchronously as soon as it   is available

I have some questions to ask about this implementation:

  • With the implementation of the async attribute, I must now stop put some scripts in <body> of my page (as in the case of jQuery UI, which is heavy), or are things different?

  • Using it may imply some performance problem or variable definitions in a certain context (for example, if I use in jQuery , there is some "excessive wait" to define the variable $ as a reference to jQuery )?

  • The assync does the same thing (or at least looks like) the definition of a callback in window.onload ?

  • What are the main advantages of using it?

  • asked by anonymous 26.01.2015 / 12:44

    1 answer

    2

    I think what you should import with the async attribute is the use of DOM manipulation by JavaScript, which could end up creating more problems if the elements are not loaded when the script needs them.

  • Yes. If you are going to use a $("input") but the input is generated after the function reaches that point, you will have problems.

  • No, async executes the code when it is completely downloaded by the browser.

  • The main advantage would be for mobile users, with faster execution of JavaScript.

  • 26.01.2015 / 12:59