Putting It in Perspective
These examples use a script that takes 10 seconds to download. Hopefully, the delay isn't as long for any scripts you use, but it is possible for a script to take longer than expected and for the user's bandwidth to affect the response time of a script. The effect that scripts have on your pages might be less than shown here, but it could still be noticeable. Having multiple scripts in your page compounds the problem.
In some situations, it's not easy to move scripts to the bottom.
If, for example, the script uses document.write
to insert part of the page's
content, it can't be moved lower in the page. There might also be
scoping issues. In many cases, there are ways to work around these
situations.
An alternative suggestion that comes up often is to use deferred scripts. The DEFER
attribute indicates that the script does
not contain document.write
, and is a
clue to browsers that they can continue rendering. You can see this in
the following example.
- Deferred Scripts
Unfortunately, in Firefox, even deferred scripts block rendering and parallel downloads. In Internet Explorer, components lower in the page are downloaded slightly later. If a script can be deferred, it can also be moved to the bottom of the page. That's the best thing to do to speed up your web pages.
Move scripts to the bottom of the page.
Get High Performance Web Sites now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.