admin管理员组

文章数量:1400161

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:

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:

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:

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:

Share Improve this question asked Mar 10, 2014 at 7:15 hh54188hh54188 15.7k35 gold badges116 silver badges192 bronze badges 1
  • 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 plete answers, though. Nice question! – Renato Zannon Commented Mar 10, 2014 at 7:17
Add a ment  | 

1 Answer 1

Reset to default 9

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.

本文标签: javascriptscript element with async attribute still block browser renderStack Overflow