Reputation: 15043
Consider the following scenario:
<h1>Hello!</h1>
<script src="cool1.js"></script>
<script src="cool2.js"></script>
<h2>Goodbye!</h2>
<img src="boat.gif" alt="Big Boat">
When cool1.js
is being downloaded does that mean that Hello!
has been displayed but Goodbye!
will not be displayed until cool1.js
downloads AND executes?
When will cool2.js
download (I know JavaScript is single threaded so it has to wait for cool1.js
to finish executing)? When will boat.gif
download and display?
Here the author claims:
The browser can only be executing JavaScript or rendering UI at any particular point in time ... think of what happens as a page downloads to the browser. The page has started to render as it was downloaded, and then a tag is encountered. At that point, the browser can no longer continue rendering because the JavaScript may affect the UI, and so it waits.
I don't get this because couldn't JavaScript modify content that came before it?
For example what if cool1.js
changed the contents inside <h1>
to say "Good day"?
Upvotes: 2
Views: 1197
Reputation: 707288
Script block is a concern because it postpones (or delays) the visibility of page elements physically located after the script in your HTML file thus delaying when those later page elements are visible to the viewer and making it take longer to display your whole page.
In this block:
<h1>Hello!</h1>
<script src="cool1.js"></script>
<script src="cool2.js"></script>
<h2>Goodbye!</h2>
<img src="boat.gif" alt="Big Boat">
Here's what you can conclusively say:
<h1>Hello!</h1>
will be in the DOM and available before the cool1.js
script runs.cool1.js
script will run before cool2.js
and both will run before the rest of the DOM is available.boat.gif
may be downloaded in parallel with other downloads (that's up to the browser), but it will not display until some time after cool2.js
has run. Older browsers probably won't even start downloading it until after cool2.js
has run.So, as far as general advice for performance optimizing your site:
See this answer to a related question for a lot of the details about loading scripts with the async
or defer
attribute: load and execute order of scripts.
Upvotes: 4
Reputation: 14645
JFriend00 already has an excelent answer, but I'm leaving my answer to answer your other part of the question: 'why would script blocking be a concern?'
Until the external javascript has downloaded, rendering on the page stalls which could potentially lose you traffic as people give up waiting.
Instead it's sometimes a good idea to load the remote Javascript asynchronously so the page can continue to render and the code is downloaded in the background.
The main point is, you need to think when specific scripts are needed and place/load/execute them accordingly (and async can make this more difficult since you might need to defer the functions in the external file from running until the page has rendered, see this page for examples how to handle this).
Upvotes: 1