[thelist] A question on optimization.

Andrew Clover and-evolt at doxdesk.com
Mon Mar 14 05:01:32 CST 2005

Volkan Özçelik <volkan.ozcelik at gmail.com>:

> 2. The scripts start loading and the images do not load until all the
> scripts load. (which might take up to 8 seconds on an 56K connection).

The reason for this is that <script>s can include document.write, which 
can output content to the current point in the page. So no further page 
parsing can take place until the script is completely downloaded, and no 
images can be loaded until their <img> tags are actually parsed.

(This doesn't happen in XHTML-parsed-as-XML on Mozilla, because you 
can't call document.write() in Mozilla XHTML-as-XML.)

If a script does not attempt to call document.write(), you can give the 
browser a hint that it doesn't need to load the whole script before 
continuing to parse the page (and load its images):

   <script type="text/javascript" src="blah.js" defer="defer"></script>

However I believe the only browser to take advantage of this possibility 
(AFAIK) is IE/Win.

For this reason, and because it's often more convenient to have all page 
elements available at script run-time (rather than relying on 
window.onload), putting <script>s at the bottom of the page is probably 
a good idea in general.

To summarise: Yes.

Andrew Clover
mailto:and at doxdesk.com

More information about the thelist mailing list