More on the defer attribute

I've corresponded with Dave Raggett, editor of the HTML specification, about the defer attribute of the <script> tag. As I mentioned previously, I don't believe that the spec, as it stands, allows deferred scripts to be executed out of order, only deferred until another non-deferred script is encountered. Although Microsoft is the only vendor to implement this attribute, my reading of the spec is that they got it wrong.

Warning: this post is about minor details in the HTML spec. You probably should stop reading now!

When I first queried Dave about this, he responded like this:

The specification doesn't impose an ordering on the execution of script elements with the defer attribute set, so it would be unadvisable to rely on the ordering in a particular browser.

Yuck. This means that using the defer attribute opens up a big implementation dependecy. So I wrote back to press my point:

I think I disagree. The spec does say: "All SCRIPT elements are evaluated in order as the document is loaded." And the description of the defer attribute doesn't explicitly state that setting the defer attribute releases the user agent from the obligation to execute scripts in the order in which they appear.

I followed with a request that the spec be clarified:

Anyway, I suppose I'd suggest that this matter be clarified the next time an update is released. Since IE is apparently the only UA that honors the defer hint, I suppose the spec should probably be clarified in their favor, and should explicitly state that scripts with the defer attribute may be executed out-of-order

Dave says he's passed my request on. We'll see what comes of it. In the meantime, I guess I've got to give Microsoft's implementation the benefit of the doubt.

I used to say that any script that does not use document.write() could be deferred. I now understand that this is not the case, at least with the IE implementation. If you set the defer attribute on a script, you must be sure that the script has no interaction with any other scripts on your page, since you cannot know what order they will be executed in. (I suppose that if you defer a script and place it after all other scripts, then your script can safely use properties defined by the non-deferred scripts before it.) Well-written unobtrusive JavaScript code is often written in the form of modules that do not depend on what comes before or after, and these modules would then be candidates for the defer attribute.

Books

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

Completely updated for Ajax and Web 2.0

"A must-have reference"
Brendan Eich,
creator of JavaScript

The classic Java quick-reference

Advertising

Pages

Hosted By

Powered by Movable Type 4.21-en