10

I use cuzillion tool build a page:

<head>
  <script async></script>
</head>
<body>
   <img />
   <img />
   <img /> 
</body>  

there is only one script element in head, with async attribute and 2 second delay, 3 second to execute.

but the page load timeline in Chrome is:

enter image description here

when the script executing, it still block the browser render process?

But why? it shouldn't execute asynchronously?

However it doesn't block the parser:

enter image description here

hh54188
  • 14,887
  • 32
  • 113
  • 184
  • AFAIK, javascript never runs in parallel with rendering the page. What you achieve is not blocking the parser, or the load of other resources. That's because async scripts can't use `document.write`. I'm curious for more complete answers, though. Nice question! – Renato Zannon Mar 10 '14 at 07:17

1 Answers1

8

The execution of any script always blocks parsing, rendering, and execution of other scripts in the same tab. The attribute async does not change that.

The only thing async does is tell the browser that the script should be fetched (assuming it's a remote file) without blocking those activities.

After the script is downloaded, the script starts executing at the next available opportunity (that is, right after the current script, if any, finishes running; a new script won't, of course, interrupt a running script). Once that happens, your rendering is blocked. So, with a fast web server, downloading happens so fast that async makes no difference at all.

If you don't want your scripts to pause rendering, use defer attribute instead of async. That will delay the execution of the script until after the document is fully loaded.

More on this here.

max
  • 49,282
  • 56
  • 208
  • 355