Home > View Post

Is a script tag slowing down your web page

When downloading a web page, if Internet Explorer hits a <script src="some url" type="text/javascript"> it has to stop processing the rendering of the page until it has downloaded and evaluated the specified script.

Why?

Because the script might contain things like document.write() instructions that have to be processed in order.

So?

Well, if there is any possibility that the download of that script might be delayed, for whatever reason, then so will the rendering of that page.

This can be a real problem if the download is significantly delayed and the script tag is near the top of the source. All the user sees is a blank page. And a delayed script might be more common than you'd think...

For example, this very site has a link to Google so we can use their analytics service. If for any reason that link is down - our users will experience a very slow theJoyOfCode.com.

OK, it's unlikely that google's site will go down. But the delay could be out of their hands. We recently had a similar problem on one of the sites I work on because our proxy server was blocking the marketing partners Url. Result? The homepage took nearly 20 seconds to display.

Oh no! What can I do about it?

Well, don't panic, There is a solution. First, you could move your script to the bottom of your source. But the best way is through the use of the defer attribute:

<script src="some url" type="text/javascript" defer="defer"> </script>

"Using the attribute at design time can improve the download performance of a page because the browser does not need to parse and execute the script and can continue downloading and parsing the page instead." (And it's valid HTML/XHTML too)

Obviously, if your script is dependant on document order (because it contains document.write() instructions or contains functions used during the rendering of your page, for example) then deferring the script won't work.

Tags: Other

 
Josh Post By Josh Twist
6:00 AM
16 Jan 2006

» Next Post: Why I love XHTML
« Previous Post: Web 2.0 on the radio

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.